0

I am getting an error message on a trigger that was working until recently. The issue arises on a trigger that inserts an OpportunityLineItem on an Opportunity. Here is the error:

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):

Here is my confusion - I have a hard coded PriceBook Entry ID in the code (for now). But this id is drawn from the correct pricebook. To check myself, I was able to manually add an Opportunity Product and I display the Pricebook ID (via formula field). It is the same value. Any thoughts? Thanks

0

From the Product and Schedule Objects Data Model:

enter image description here

The PricebookEntry used by the OpportunityLineItem must have the same Pricebook2Id as the Opportunity.Pricebook2Id that you are adding it to.

E.g.

Id opportunityId = '006000000000001';
Id pricebookEntryId = '01u000000000001';

Opportunity opp = [Select Id, Pricebook2Id from Opportunity where Id = :opportunityId];
PricebookEntry pbe = [Select Id, Pricebook2Id from PricebookEntry where Id = :pricebookEntryId];
System.assertEquals(opp.Pricebook2Id, pbe.Pricebook2Id, 'OpportunityLineItem.PricebookEntry.Pricebook2Id should equal  OpportunityLineItem.Opportunity.Pricebook2Id');
| improve this answer | |
  • Yes, it did. I had no problem entering that same product from the same pricebook manually. I changed it all to a query and the issue went away. – jaw999 Mar 6 '19 at 14: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.