function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dai tran 6dai tran 6 

How can add 2 Products have 2 Price book to a Order?

I have 2 Products: 
Product 1 -> Standard Price -> 200$
Product 2 -> Promotional price -> 100$

How can add 2  this Products into a Order?
insert order:
          Order od=new Order();
          od.AccountId=con.AccountId;
          //od.ContractId=hd.Id; 
          od.EffectiveDate=system.today();
          od.Status='Draft';
          insert od;

insert order item:
string searchquery='select ProductCode,Name,ImageName__c,(Select UnitPrice,Pricebook2.Name From PricebookEntries where IsActive=True  Order By UnitPrice ASC )  from Product2 where ProductCode in ('+SOQL_IDs+')'; 
              List<Product2>  MyProducts= Database.query(searchquery); 
              List<OrderItem> lsDetail=new List<OrderItem>();
              for(Integer i=0;i<MyProducts.size();i++)   
              {   if(MyProducts.get(i).PricebookEntries !=null && MyProducts.get(i).PricebookEntries.size()>0)
                  {
                      String code=MyProducts.get(i).ProductCode;
                      String sl=listCarts.get(code);
                      OrderItem ode=new OrderItem();
                      ode.OrderId=od.Id;
                      ode.Quantity=Double.valueOf(sl);
                      ode.PricebookEntryId=MyProducts.get(i).PricebookEntries.get(0).Id;
                      ode.UnitPrice=MyProducts.get(i).PricebookEntries.get(0).UnitPrice;
                      ode.Product2Id=MyProducts.get(i).Id;
                      lsDetail.Add(ode);
                      itotalMoney=itotalMoney+ ode.Quantity * ode.UnitPrice;
                  }
                 
              }
              
               insert lsDetail;

It occur error:
​Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: []


 
Ajay K DubediAjay K Dubedi
Hi Dai,

Please go through the below link. Hope this helps you.

You need to set the PriceBook2Id on order.

https://salesforce.stackexchange.com/questions/49472/problem-with-inserting-a-new-order-item
https://salesforce.stackexchange.com/questions/202437/unable-to-set-pricebook-id-in-orderitems/202444

Please select as best answer if it helps you.

Thank You,
Ajay Dubedi
dai tran 6dai tran 6
I have 2 Products: 
Product 1 -> Standard Price -> 200$
Product 2 -> Promotional price -> 100$

=> I have 2 PriceBook2Id  =>Have i to insert 2 orders?
Ajay K DubediAjay K Dubedi
Hi Dai,

Either you create two orders or create only one order based on condition assign PricebookId.

Thank You,
Ajay Dubedi
dai tran 6dai tran 6
I want create a Order, But i have 2 PricebookID, Can it?
You can explain a little about order management?
If a Cart has 2 Products: 
Product 1 -> Standard Price (PricebookId =1) -> 200$
Product 2 -> Promotional price (PricebookId =2)  -> 100$
Because if set PricebookId to Order, i have to create 2 Orders.
I feel not fine