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?
2 Answers
All fields on the Triggering object are available, but if you want any parent or child records, you will have to query for them
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()
];