4

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

  • possible duplicate of Test code for the new standard Order object – Daniel Ballinger Jun 10 '14 at 9:18
  • @Daniel Ballinger, I view that post "Test code for the new standard Order object" in that post they mention PriceBook2Id field in Order object but in my sandbox PriceBook2Id field is not available in Order object, Is there any other way to findout the PriceBook2Id field in order object. – Venkatsforce Jun 10 '14 at 9:30
  • What happens if you add obj.Pricebook2Id = pbook.Id; before `insert obj;'? The Order.Pricebook2Id field appears in the docs as required. – Daniel Ballinger Jun 10 '14 at 9:33
  • It Shows "Invalid field Pricebook2Id for SObject Order" – Venkatsforce Jun 10 '14 at 9:38
  • I've reworked your question to make it clearer that you can't set the required Pricebook2Id for the Order SObject. – Daniel Ballinger Jun 10 '14 at 9:56
5

Change the API Version of your Test-Class to 28.0 (or higher). That worked for me.

  • Thanks for your support @FellowDeveloper, now its working. – Venkatsforce Jun 12 '14 at 6:21
  • Good catch. This is something to watch out for if you are using an IDE that hasn't been updated to the latest release or the test class meta.xml has an older version in it. – Daniel Ballinger Jun 12 '14 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.