I am developing the custom aura component for adding products to the opportunity for achieving client requirements. For this, I have created a custom price book entry object but while creating the opportunity Line item record it gives the "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Entry ID: id value of incorrect type" error. Because Opportunity Line Item has a PricebookEntryId field lookup to Pricebook Entry object and which is required. Please help me to solve this issue.
2 Answers
You can't put in any arbitrary Id into a lookup field and expect it to work. You'll want to create a new Pricebook2 record for each Opportunity, put it into the standard Pricebook2Id field on the Opportunity, then create your PricebookEntry records for each product you want to attach, and finally create the OpportunityLineItem entries using the new PricebookEntry records you created. You can do this by writing a custom UI, or by using triggers. You can use a before insert trigger on Opportunity to create a new PriceBook2, and then use a before insert trigger on OpportunityLineItem to create PricebookEntry values just-in-time using other custom lookup fields, or some other method.
OpportunityLineItem
objects must reference a PricebookEntry
object. That is a required field per the schema
Creating custom pricebook entry-ish sobjects and expecting them to work with the OOTB objects:
OpportunityLineItem
QuoteItem
OrderItem
- ...
is not a road you want to go down.