I have a trigger on an object ('item'), which is a child of 'parent', which is in turn a child of 'grandparent'.

So a SOQL query involving item.parent__r.grandparent__c normally returns the ID of the related grandparent record.

My problem arises when working with an AfterDelete trigger context, because I can only work with trigger.old.

The record info pulled by the trigger is limited to the object itself, so item.parent__r.grandparent__c isn't accessible without querying.

But, I can't actually query that grandparent relationship since the record is now deleted:

[SELECT ID, parent__r.grandparent__c FROM Item WHERE ID IN :trigger.Old]

returns nothing, because there ARE no more Item records with that ID, because they are deleted at this point.

My current workaround is to build a list of parent IDs, since those values are included in trigger.old, then to query the grandparent IDs using a query on the parent object.

I'm wondering: is there a better way to get multi-level relationship info in this context?


I'm assuming the deleted record would be in the recycle bin, in which case all you need do is use QueryAll in your query. That will allow you to access the records that are in the recycle bin and have them included as part of your query.

  • 1
    Yep that did the trick! – smohyee Aug 4 '16 at 20:48

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.