11

I'm sure this has been addressed before but I can't seem to find the answer. In a trigger on a record, aside from the actual fields on the record, what do I have access to? The parent info? that record's children? Do you just add the fields by re querying in your handler?

1
  • 2
    Now we should have a definitive dupe.
    – Adrian Larson
    Commented May 4, 2016 at 20:42

2 Answers 2

9

All fields on the Triggering object are available, but if you want any parent or child records, you will have to query for them

0
15

You only have access to fields that are on the object. If you want parent or child data, you have to query for it. In either case, you can query at any of the three levels. For example, if you have Child, Parent, and Grandparent objects and you are in the Parent trigger:

List<Grandparent__c> parents = [
    SELECT Name
    FROM Grandparent__c
    WHERE Id IN :someCollection
];
List<Parent__c> contextRecords = [
    SELECT Grandparent__r.Name, (SELECT Name FROM Children__r)
    FROM Parent__c
    WHERE Id IN :trigger.newMap.keySet()
];
List<Child__c> children = [
    SELECT Name
    FROM Child__c
    WHERE Parent__c IN :trigger.newMap.keySet()
];

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .