2

In the answer to this question: Maintaining a reference to sObjects being upserted for subsequent related object insertion

There is a suggestion to: What about building an OLI External Id, stuffing it into a plain text field on the child, and then in the child trigger go find the related OLI an sets it's actual Id value? Guess that presumes you put the OLIs in first... –

This is exactly my problem, and exactly how I am trying to solve it, but I am stuck. Any idea how to accomplish this?

To flush out the details further, an After trigger runs on the OpportunityLineItem and creates a Commission__c record (custom object). Because you aren't allowed to lookup to Opportunity Line Items I am fine with just pasting in the Opportunity Line Item to the newly created Commission__c record.

However, I would like to accomplish what was suggested for the question linked above, by then running an after Insert trigger on the Commission__c record to find the Opportunity Line Items that match the pasted in Opportunity Line Item Id on the Commission__c record. I want to then update the matched Opportunity Line Items by updating the lookup field on the Opportunity Line Item to the related custom object that had the Opportunity Line Item id match.

I can query the Opportunity Line Items, but then how do I insert the id of the related record that on which it matched?

By design, but not forced, an Opportunity Line Item should be one-to-one with the related custom object record.

1

So you just need to link (via lookup field) to the newly created Commission__c from the LineItem record? I think you can just do it in a before trigger for OpportunityLineItem:

  1. Create Commission__c (insert) - you automatically get the Id back for any insert
  2. Take the Id from Commission__c's Id and stuff it in the lookup field of OLI
  3. Since it's a before trigger, you're done - no separate update needed!

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.