Could some body please help me on this? I am improving test coverage and below is my code:

static testmethod void opportunityLineItem_Test(){
    Id priceBookID = Test.getStandardPricebookId();

    Opportunity oppObj = new Opportunity(Name='test opportunity',StageName = 'Pending Renewal', CloseDate = Date.newInstance(2015, 06, 30));
    insert oppObj;

    Product2 testPrd = new Product2(IsActive=true,Name='test Product',Description='Description for test Product');
    insert testPrd;

    PricebookEntry pbe = new PricebookEntry(IsActive=true,Pricebook2Id=priceBookID,Product2Id=testPrd.Id,UnitPrice=10.00);  
    insert pbe;

    OpportunityLineItem oli = new OpportunityLineItem(OpportunityId = oppObj.Id, PricebookEntryId = pbe.Id, Quantity = 10.00, ServiceDate = Date.newInstance(2015, 06, 24),totalPrice = 20.00);
    insert oli; //It is failing on this line


The error I am receiving is : System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: unknown (pricebook entry is in a different pricebook than the one assigned to the opportunity): [unknown]

My code is same as the one in this post(OpportunityLineItem in test classes) but not sure why this is still failing.

EDIT: I just noticed that I have a process running on opportunity object, which will assign a default price book whenever a new opportunity object is inserted. This is why my test is failing saying..

  • 1
    Are you sure you are running this test class, when I copy paste the exact code I do not see any issues/
    – Rao
    Jun 25 '15 at 18:26
  • Yes, I am running in test class.
    – javanoob
    Jun 25 '15 at 18:40
  • can you paste your developer console and the debug log
    – Rao
    Jun 25 '15 at 18:43
  • @rao Your guess is correct. There is nothing wrong with the code. I just edited the question. I wish i could accept your comment as answer :)
    – javanoob
    Jun 25 '15 at 18:54
  • This was an unique scenario I would have not guessed unless looking carefully at the debug log. Answer this question and mark your answer solved for the benefit of others who come looking for answers.
    – Rao
    Jun 25 '15 at 19:01

The Opportunity needs to have pricebook2Id set

Opportunity oppObj = new Opportunity(Name='test opportunity',
                                     StageName = 'Pending Renewal', 
                                     pricebook2Id = pricebookId,  // ***
                                     CloseDate = Date.newInstance(2015, 06, 30));

In the UI, when you add Opportunity Products, you must select a pricebook and SFDC updates the Opportunity record with that value before you can add OLI

Other things to think about:

If you use custom pricebooks, you need PBE for both the standard Pricebook and the custom pricebook

If using multi currencies; the Opportunity currency has to be the same as all OLI currencies on that Oppo.

  • 1
    Even after adding the pricebookID to the opportunity object, I am still getting the same error. Also I noticed that the link I posted in the question does not have any pricebook assigned to the opportunity object but looks like it might have worked him but mine is failing.
    – javanoob
    Jun 25 '15 at 18:39
  • I don't think the answer is correct. It doesn't work for me I am still getting same error described in above issue
    – Victor
    Jul 29 '20 at 4:17

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.