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.