Parent and children relationships accessible via trigger?

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()
];

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

Tags:

Soql

Apex

Trigger