Experience Salesforce

Parent to Child – Relationship Queries in SOQL

What You’ll Learn

  • What Is Parent-To-Child Relationship Query?

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:

  1. Project (Parent) β†’ Task (Child)
  2. Department (Parent) β†’ Employee (Child)
  3. 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
Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img
Rakshabandhan Sale