How to Access Parent and Child Relationships in Salesforce Triggers?
When writing a trigger in Salesforce, you often work with records of a particular object type. By default, the trigger context variables (such as Trigger.new
or Trigger.old
) give you access only to the fields directly stored on those records. They do not automatically include related parent or child records. This means that if you need information about parent objects or child records, you must explicitly query them within your trigger handler.
The first approach is to build a JSON structure in Apex from the Field Set and then parse it on the client-side controller to create the appropriate Lightning inputs. Below is an example implementation.
In Salesforce triggers, you can access only the fields available on the object the trigger is executing for. Parent or child relationships are not automatically populated in trigger context variables. If you want to work with these relationships, you must perform explicit SOQL queries inside your trigger handler.
For example, suppose you have three custom objects: Grandparent__c, Parent__c, and Child__c. The relationship is as follows: a Parent__c record has a lookup to Grandparent__c, and Child__c records are related to Parent__c via a child relationship. If you are writing a trigger on the Parent__c object and want to access both the parent (Grandparent) and children (Child) data, you need to query them explicitly.
// Query Grandparent records for relevant Parent records
List<Grandparent__c> grandparents = [
SELECT Name
FROM Grandparent__c
WHERE Id IN :someCollection
];
// Query Parent records including parent relationship and children relationship data
List<Parent__c> contextRecords = [
SELECT Name, Grandparent__r.Name, (SELECT Name FROM Children__r)
FROM Parent__c
WHERE Id IN :Trigger.newMap.keySet()
];
// Query Child records related to the current Parent records
List<Child__c> children = [
SELECT Name
FROM Child__c
WHERE Parent__c IN :Trigger.newMap.keySet()
];
Code Explanation:
In the first query, we fetch the relevant Grandparent__c records. We use WHERE Id IN :someCollection
to filter only the records we need. This avoids unnecessary queries and improves performance.
The second query fetches Parent__c records in the context of the trigger. The query retrieves the Grandparent__r.Name
field, which is a parent relationship field (via the lookup relationship). It also retrieves child relationship records using a nested query (SELECT Name FROM Children__r)
. The child relationship name (Children__r
) is defined in Salesforce schema and can be checked in the object manager under relationship names.
The third query retrieves the Child__c records linked to the parent records in the trigger using the Parent__c
lookup field. Again, Trigger.newMap.keySet()
provides the set of IDs for records currently being processed, ensuring the query is limited to relevant data.
Summary:
In short, Salesforce triggers give you direct access only to the fields on the triggering object. To access parent or child data, you must perform additional SOQL queries inside your trigger or handler. This gives you the flexibility to retrieve exactly the fields and relationships you need while avoiding unnecessary processing.
Enroll for Career-Building Salesforce Training with Real-Time Projects
Our Salesforce Course is meticulously designed to offer an in-depth understanding of the Salesforce platform, providing you with the crucial skills needed to excel in the CRM domain. The program includes essential modules like Salesforce Admin, Developer, and AI, integrating foundational theory with practical, hands-on experience. By engaging in live projects and real-world assignments, you’ll develop the expertise to solve complex business challenges with confidence, using Salesforce solutions. Our experienced instructors ensure you acquire both technical knowledge and valuable industry insights, enabling you to thrive in the Salesforce ecosystem.
In addition to mastering technical concepts, our Salesforce Training in Thiruvananthapuram offers personalized mentorship, exam preparation, and interview coaching to boost your career prospects. You’ll have access to comprehensive study materials, practical project experience, and consistent support throughout your learning journey. Upon completion, you’ll be fully prepared for certification exams and equipped with the practical problem-solving skills employers highly value. Embark on your Salesforce career with us—Sign up for a Free Demo today!