I want to insert the OrderItem object using Order, Pricebook2 and PricebookEntry. I retrieved the standard Pricebook2 and inserted an Order and pricebookentry.
At the time of inserting the "OrderItem" it throws the exception as:
Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: []
My Code is:
List<Account> acclist = [SELECT Id,Name FROM Account WHERE Name='Testname'];
List<Product2> lstproduct = [SELECT Id,Name FROM Product2 WHERE Name='Test'];
Pricebook2 pbook = [select id from Pricebook2 where IsStandard = true limit 1];
String Quantity = '3';
Order obj=new Order();
obj.AccountId = acclist[0].Id;
obj.EffectiveDate = Date.today();
obj.Status ='Draft';
//obj.Pricebook2Id = pbook.Id; // Results in "Invalid field Pricebook2Id for SObject Order"
System.debug('OBJ####'+obj);
insert obj;
PricebookEntry pbe = new PricebookEntry();
pbe.Pricebook2Id = pbook.Id;
pbe.Product2Id = lstproduct[0].id;
pbe.IsActive = true;
pbe.UnitPrice = 110.0;
insert pbe;
OrderItem oobj=new OrderItem();
oobj.PricebookEntryId =pbe.Id;
oobj.OrderId = obj.Id;
oobj.Quantity = Integer.ValueOf(Quantity);
insert oobj;
As per, Test code for the new standard Order object, if the following line is added before the insert obj;
line:
obj.Pricebook2Id = pbook.Id;
the following error is returned:
Invalid field Pricebook2Id for SObject Order
obj.Pricebook2Id = pbook.Id;
before `insert obj;'? The Order.Pricebook2Id field appears in the docs as required.