Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

The last line insert ordPd causes exception: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: []

All lines before the last line are all ok, and have confirmed again and again.

Have tried whatever I can do, still can't resolve the problem. It seems the pricebook and order are not related, however, they should be related by the last 2 line.

Also, Have done research and use 'SeeAllData=true' tag.

The code:

Account acct = new Account(Name='tAccount');
insert acct;

Contract ct = new Contract();
ct.AccountId = acct.Id;
ct.Status = 'Draft';
ct.StartDate = Date.Today();
ct.ContractTerm = 12;
insert ct;

ct.Status = 'Activated';
update ct;

Order od = new Order();
od.Name = '14TPE000001';
od.AccountId = acct.Id;
od.EffectiveDate = Date.Today();
od.ContractId = ct.Id;
od.Status = 'Draft';
insert od;

Product2 pd = new Product2(Name='Pord A',isActive=true);
pd.IsActive = true;
insert pd;

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
Id standardPriceBookId = pb2Standard.Id;

PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
insert pbe;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :pd.Id];
System.assertEquals(pd.Name,p2ex.Name);

OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=od.Id, Quantity=1, UnitPrice=99);
insert ordPd;
share|improve this question
1  
When Summer '14 comes out you will not have to use SeeAllData=true anymore. There is a new method that will get you the standard pricebook Id: Test.getStandardPricebookId() . –  Peter Knolle Jun 10 at 11:12

2 Answers 2

up vote 5 down vote accepted

The variable od must have it's Pricebook2Id set before inserting the record. This the same design functionality as the Opportunity and Quote objects.

share|improve this answer
    
Yes, that's it.... too stupid I am –  Cray Kao May 12 at 2:58
1  
@CrayKao It's just a learning curve. You'll get there. –  sfdcfox May 12 at 3:03
    
i also facing same problem System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: [] but i dont have a Pricebook2Id field in order object . how to resolve this error? –  nagarjuna Jun 9 at 14:40
    
@nagarjuna You should probably check for existing questions on this, but you definitely should have a Pricebook2Id field on Order (it may not be listed specifically as such on the GUI, but it should exist). –  sfdcfox Jun 10 at 0:24
    
There appear to be several comments that the Order.Pricebook2Id field isn't always available. I've reworked a newer question to deal with this separately. –  Daniel Ballinger Jun 10 at 9:52

The minimum working test code:

PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true];
Id standardPriceBookId = pb2Standard.Id;

PricebookEntry pbe = [ SELECT Id,Product2Id,Pricebook2Id,UnitPrice FROM PricebookEntry WHERE Pricebook2Id = :standardPriceBookId AND isActive=true LIMIT 1 ];

Order od = [ SELECT Id,Pricebook2Id FROM Order LIMIT 1 ];
od.Pricebook2Id = pb2Standard.Id;
update od;

OrderItem ordPd = new OrderItem(PriceBookEntryId=pbe.Id, OrderId=od.Id, Quantity=1, UnitPrice=pbe.UnitPrice, ServiceDate=Date.Today());
insert ordPd;

The last 3-5 lines are key to resolve the problem.

Ref: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_orderitem.htm

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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