Parent to Child – Relationship Queries in SOQL
Chapter Topics
- SOQL Basics (Salesforce Object Query Language)
- How to Write SOQL in APEX
- SOQL Variable Binding in APEX
- SOQL Keywords
- SOQL Date Literals In Salesforce
- SOQL Aggregate Query
- Child to Parent Relationship
- Parent to Child – Relationship Queries in SOQL
- SOQL Multi level Relationships
- SOQL Return Type
- Salesforce Dynamic SOQL
- SOQL ’for’ Loops
Understanding relationships between different objects is important to managing Salesforce data effectively. One of the most common and powerful relationships is the Parent-to-Child relationship, which enables you to organize and structure data in a way that reflects real-world scenarios. This relationship type helps in building robust data models that support complex business processes. A parent-to-child relationship query in SOQL is used to retrieve child records associated with a parent record. In this chapter, we will look at how you can use this query to fetch data from standard as well as custom objects.
What is a Parent-to-Child Relationship Query?
A Parent-to-Child relationship query in Salesforce allows you to retrieve data from related objects where the parent object has a one-to-many relationship with the child object. This means that for each record in the parent object, there can be multiple related records in the child object. Parent-to-child relationship queries allow you to efficiently retrieve data from both the parent and child objects in a single SOQL query, making it convenient to access related records.
Example:
In the case of an Account object (parent) and a Contact object (child) the Account can have multiple associated Contacts. Using a Parent-to-Child relationship query, you can retrieve an Account and all related Contacts in a single query.
For Standard Objects
There are some pre-defined relationships in Salesforce between many standard objects. Such as:
- Account (Parent) → Contact (Child)
- Opportunity (Parent) → Opportunity Product (Child)
- Case (Parent) → Case Comment (Child)
Example SOQL Query:
List<Account> AccountData = [SELECT Id, Name, Industry, AnnualRevenue, NumberOfEmployees, (SELECT FirstName, LastName FROM Contacts) FROM Account] |
List<Opportunity> OpportunityData=[SELECT Id, Name, StageName, Amount, CloseDate , (SELECT Id, OpportunityId, Name FROM OpportunityLineItems) FROM Opportunity] |
List<Case> CaseData = [SELECT Id, ParentId, Subject, Description, Priority, (SELECT Id, IsPublished, CommentBody FROM CaseComments) FROM Case] |
For Custom Objects
For custom objects, the query format is similar but requires the use of the __r suffix for the child relationship name:
Example of Custom Objects:
- Project (Parent) → Task (Child)
- Department (Parent) → Employee (Child)
- Bank (parent) → Branch (child)
SELECT Bank_Name__c, (SELECT Branch_Id__c, Name, State__c FROM Branches__r) FROM Bank__c |
This custom query will fetch:
- The Bank_Name__c field from the Bank__c object (parent)
- A subquery that selects Branch_Id__c, Name, and State__c fields from the Branches__r object (child) that are related to each Bank__c record
- The __r suffix is used for the child relationship name for custom objects.
![Start Your Salesforce Developer Journey CTA](https://s2-labs.com/wp-content/uploads/2024/06/Start-Your-Salesforce-Developer-Journey-CTA.webp)
Need more support?
Get a head start with our FREE study notes!
Learn more and get all the answers you need at zero cost. Improve your skills using our detailed notes prepared by industry experts to help you excel.