I see this question once in awhile on the Salesforce.com message boards so I thought I'd put something together. So in this scenario you have a Sales_Order__c
custom object which has a lookup relationship to Opportunity. When processing the Sales_Order__c
records in your trigger, you want to access some fields on the Opportunity via the relationship. Your trigger might look something like this:
However, every time you run the Trigger, the Opportunity is null even though there is a valid Id in Opportunity__c
. The reason is that for scalability, the Force.com platform doesn't perform an in-memory lookup for each relationship in your object. You need to do that yourself. The good thing is that the solution is relatively painless and is safe for bulk transactions.
In the trigger below you first want to create a set of unique Opportunity Ids that are in the trigger context. You then use those Ids to query and create a map with the Opportunity Id as the key and the Opportunity object as the value. Then when you process your Sales_Order__c
records you can access the Opportunity object from the map by its Id (line 14).