+ Start a Discussion
Linda 98Linda 98 

Create opportunity with line items from order

i am having a custom button which opens VF page.and creates opportunity from order and orderitem .I also wan to copy line items of order in to opportunitylineitems.
Plese help.I am having all ready except copying lineitems.
thanks in advance...
Best Answer chosen by Linda 98
goabhigogoabhigo
You will need to query PriceBookEntryId too from OrderItem object. This is the one which connects OrderItem to Product.
public with sharing class convertordertooppcontroller{ 
     public ApexPages.StandardController newpage; 
    
     public convertordertooppcontroller(apexpages.standardcontroller stdcontroller) { 
          this.newpage=stdcontroller; 
     }

     public pagereference createopportunity() {
          Opportunity newopp =new Opportunity();
          Order o =[select Status,Account.name,enddate from Order where Id =:newpage.getId()];
          List<Orderitem> listOI =[select id,unitprice,Description,quantity, PriceBookEntryId from OrderItem where orderid =: newpage.getId()];
          newopp.stagename=o.status;
          newopp.Name=o.Account.Name;
          newOpp.closedate=o.EndDate;
          insert newOpp;
          for(OrderItem oi : listOI) {
               OpportunityLineItem oli = new OpportunityLineItem();
               oli.OpportunityId = newOpp;
               oli.PriceBookEntryId = oi.PriceBookEntryId;
               oli.UnitPrice = oi.UnitPrice;
               oli.Description = ol.Description;
               oli.Quantity = oi.Quantity;
               listOLI.add(oli);
          }
          insert listOLI;
          Pagereference pg =new pagereference('/'+newOpp.id);
          pg.setredirect(true);
          return pg;
     }
}

I am assuming you need to create one opportunity per Order, and this happens one Order at a time. Hence I have removed List<Opportunity>.

I have not tested this code, so let me know if you are finding any trouble.

--
Abhi

All Answers

goabhigogoabhigo
Can you paste th code that has been written? It shouldn't be tough.
But out of curiosoty, why would you create Order from Opportunity??
Linda 98Linda 98
public with sharing class convertordertooppcontroller{
  public ApexPages.StandardController newpage;
  List<Opportunity> oplist =new List<Opportunity>();
  Map<id,List<orderitem>> ordermap= new Map<id,List<orderitem>>();
  public convertordertooppcontroller(apexpages.standardcontroller stdcontroller){
  this.newpage=stdcontroller;
  }
    Public pagereference createopportunity() {
    Opportunity newopp =new Opportunity();
    Order o =[select Status,Account.name,enddate from Order where Id =:newpage.getId()];
    List<Orderitem> oi =[select id,unitprice,Listprice,Description,quantity from orderitem where orderid =: newpage.getId()];
    newopp.stagename=o.status;
    newopp.Name=o.Account.Name;
    newOpp.closedate=o.EndDate;
    oplist.add(newOpp);
    insert oplist;
    pagereference pg =new pagereference('/'+newOpp.id);
    pg.setredirect(true);
    return pg;
    }

}
:) its business req.Not sure

This is code which creates opportunity when clicked on custom button which links to a VF page.

But it doesnt create lineitems.I even want to copy line items.

Thank you!!

 
goabhigogoabhigo
You will need to query PriceBookEntryId too from OrderItem object. This is the one which connects OrderItem to Product.
public with sharing class convertordertooppcontroller{ 
     public ApexPages.StandardController newpage; 
    
     public convertordertooppcontroller(apexpages.standardcontroller stdcontroller) { 
          this.newpage=stdcontroller; 
     }

     public pagereference createopportunity() {
          Opportunity newopp =new Opportunity();
          Order o =[select Status,Account.name,enddate from Order where Id =:newpage.getId()];
          List<Orderitem> listOI =[select id,unitprice,Description,quantity, PriceBookEntryId from OrderItem where orderid =: newpage.getId()];
          newopp.stagename=o.status;
          newopp.Name=o.Account.Name;
          newOpp.closedate=o.EndDate;
          insert newOpp;
          for(OrderItem oi : listOI) {
               OpportunityLineItem oli = new OpportunityLineItem();
               oli.OpportunityId = newOpp;
               oli.PriceBookEntryId = oi.PriceBookEntryId;
               oli.UnitPrice = oi.UnitPrice;
               oli.Description = ol.Description;
               oli.Quantity = oi.Quantity;
               listOLI.add(oli);
          }
          insert listOLI;
          Pagereference pg =new pagereference('/'+newOpp.id);
          pg.setredirect(true);
          return pg;
     }
}

I am assuming you need to create one opportunity per Order, and this happens one Order at a time. Hence I have removed List<Opportunity>.

I have not tested this code, so let me know if you are finding any trouble.

--
Abhi
This was selected as the best answer
goabhigogoabhigo
Oops..

Please add, List<OpportunityLineItem> listOI = new List<OpportunityLineItem> (); in line no. 3.
Linda 98Linda 98
I was missing pricebookentryid so was not able to get that.
Thanks a lot:)
goabhigogoabhigo
Happy to know I was able to help you :)
Linda 98Linda 98
Also i am trying to get opportunity field(look up) filled and vice versa. So trigger is the only option???if so what conditions i have to use?? I should get that field only when opportunity is created when clicked that button...when it is createe from order object.not all the times.. Any pointers please!!!! Thank you
goabhigogoabhigo
Change line no. 18 to -   oli.OpportunityId = newOpp.Id;. Again, I missed this out, since I did not test it. 
Yes to fill Lookups, you need to write Apex code (or you go for VisualFlow).
Linda 98Linda 98
Yes i already had written that.
My break was at linking opportunity to order.I have to fill that only when opportunity is created by clicking the button.
i am writing a trigger for that but how can i say that thos opp is created by clicking button without creating any custom fields or any.

thank you
Linda 98Linda 98
Also could you please help me for capturing the Opportunityid which is newly created by button click.i am struck even there in my trigger.
goabhigogoabhigo
Where do you want to use the OpportunityId? As mentioned above, newOpp.Id will give you the ID of newly created Opportunity after you have clicked on the custom button.
Linda 98Linda 98
No I am actually trying to link order object to opportunity and opportunity to order. To link newly createe opportunity to order and to fill the lookup field i need a trigger . Right??? For that i need to capture the opportunity id and query it then update order object by filling opportunity field. Right? Please correct me if i am wrong.
goabhigogoabhigo
No, you can do that in the same class. Just update the Opportunity field in Order object.
So you will need one lookup in Order pointing to Opportunity and one lookup field in Opportunity pointing to Order.

It would be something like:
newOpp.Order__c = newpage.getId();
....
...
o.Opportunity__c = newOpp.Id;
update o;
Linda 98Linda 98
Thank you That works perfectly
goabhigogoabhigo
Glad that it worked. Happy to help :)
NIRAJ JADHAVNIRAJ JADHAV
Hi Goabhigo,
Thanks a lot for your support.
I want to do the same stuff from Opportunity (i.e. create Order and Order Line Items from Opportunity).
I have customized your Apex Class where I have replaced Orders with Opportunities and Orders Line Items with Opportunity Line Items to create Order and Order Line Items from Opportunity, which then I have then called it via Visualforce Page, however, I am getting VFPage only with different Tabs but no appropriate results.
 
Apex Class:-
 
public with sharing class ConvertOppToOrdersController{ 
     public ApexPages.StandardController newpage; 
    
     public ConvertOppToOrdersController(apexpages.standardcontroller stdcontroller) { 
          this.newpage=stdcontroller; 
     }
 
     public pagereference createorder() {
          Order neworder = new Order();
          Opportunity opp = [select StageName,Account.name, CloseDate from Opportunity where Id =:newpage.getId()];
          List<OpportunityLineItem> listOI =[select id,unitprice,Description,quantity, PriceBookEntryId from OpportunityLineItem where opportunityid =: newpage.getId()];
          neworder.status=opp.StageName;
          neworder.Name=opp.Account.Name;
          neworder.EffectiveDate=opp.CloseDate;
          insert neworder;
          for(OpportunityLineItem oi : listOI) {
               OrderItem oli = new OrderItem();
               oli.orderId = neworder.name;
               oli.PriceBookEntryId = oi.PriceBookEntryId;
               oli.UnitPrice = oi.UnitPrice;
               oli.Description = oi.Description;
               oli.Quantity = oi.Quantity;
               insert oli;
               }
          Pagereference pg =new pagereference('/'+neworder.id);
          pg.setredirect(true);
          return pg;
}
}
 
VF Page:-
<apex:page standardController="Opportunity" extensions="ConvertOppToOrdersController">
</apex:page>