You need to sign in to do that
Don't have an account?
How to Retrieve data from a related object in a After event trigger? (with NO queries)
Hi everyone,
I need to get some data from a related object through a lookup field in a trigger. I expected that in a Before event the only way to get data from the related object was by using a query. But in an AFTER event all relationships should be established and related could be get from lookup fields.
Here is my sample trigger code:
for(integer i = 0; i < trigger.new.size(); i++){ if(trigger.IsAfter && (trigger.IsInsert || trigger.IsUpdate)){ childObject__c RecordNew = trigger.new[i]; if(!RecordNew.ChildLkpField__r.ParentLkpField__r.ParentParentCheckbox__c && RecordNew.ChildLkpField__r.ChildCheckbox__c){ if(trigger.IsInsert){ // more code }else if(trigger.IsUpdate){ // more code } } }}
Suppose the lookup field name is "ChildLkpField__c", the poblem is that when I compile the code it doesn´t show any syntax error and when I print the values of the ChildLkpField__r."checkboxes" fields are shown as false and never like null.
Does anybody know how to catch the real value of the parent object fields without quering it (in a after event trigger)? If not possible please let me know. Or is it a Salesforce bug?
Thanks,
Wilmer
All Answers
The related data won't be auto-populated in the trigger. You will have to query for related data to populate the relationships.
Hi Albert,
Thanks for your quink answer.
I have this doubt because according to Apex documentation on page 73 (Triggers and Order of Execution), after a record is saved in the database, all after trigger events are executed then some more things and finally commited. So I expected after the record is saved all relationships are established.
Does it mean relationships are set after the record is commited? In that case why is not possible with after update triggers?
Regards,
Wilmer
well, I don´t like to see that, but if that's reality what else could be done.
Thanks for your reply.