I have a custom object SapLine which is like opportunityLine, i.e has a product2 lookup field. The hierarchy is Opportunity-->OpportunityLineItem, SapOrder-->SapLine. I created a PricebookId field on SapOrder, which is populated from Opportunity.Pricebook2Id.
And two triggers:
After insert on OpportunityLineItem - creates a SapLine and populates the product field with a product from OpportunityLineItem. Works fine.
After insert on SapLine - creates an OppLine and populates fields, the problem is that I cannot simply pass Product2Id, because the field is not writable. I need to use PricebookEntryId field but how do I get the pricebookEntryId from SapLine?
I thought of adding a before insert on SapLine where you would populate a custom field pricebookEntryId, to later use it for OppLine.PricebookEntryId using the following method:
Having SapOrder.PricebookId and SapLine.Product2Id I can SOQL PricebookEntry
[SELECT id FROM PricebookEntry WHERE Product2id IN: lstOfProducts AND Pricebook IN: lstOfPricebooks]
How do I map pricebookEntryId to a specific SapLine?