Gem of Salesforce Summer'23 Release - Query Five Levels of Parent-to-Child Relationships - Know How
Feature
One of the gems of Salesforce Summer'23 Release is Query Five Levels of Parent-to-Child Relationships in SOQL Queries. Summary of this feature is: -
SOQL now supports relationship queries that traverse up to five levels of parent-child records. Use a single SOQL query to get parent-child records from five different levels. This ability is limited to SOQL queries via the REST and SOAP query calls on standards and custom objects.
Earlier, you could only query two levels of Parent-to-Child Relationships in SOQL Queries.
Gotcha
This ability is limited to SOQL queries via the REST and SOAP query calls on standards and custom objects.
In Salesforce Apex class, the limit for SOL queries is still only two levels of Parent-to-Child relationships.
Know How
I decided to take a stab on this awesome and powerful feature and see how it works.
Data Model
I created a five level Parent-to-Child Object Model as shown below.
Data Load
For this exercise, I loaded a few thousands of records using data loader. Summary of data volume: -
Query
I used below query for this exercise: -
SELECT Name
? ? (
? ? SELECT id,LastName,
? ? ? ? (
? ? ? ? SELECT Id, Name,
? ? ? ? ? ? (
? ? ? ? ? ? Select Id, Name,
? ? ? ? ? ? ? ? ? ? (
? ? ? ? ? ? ? ? ? ? Select Id, Name from Level3__r
? ? ? ? ? ? ? ? ? ? )?
? ? ? ? ? ? ? ? ? ?from Level2__r
? ? ? ? ? ? )
? ? ? ? FROM Level1__r
? ? ? ? )
? ? FROM Contacts
? ? )
FROM Account
Callout Tool
I used Workbench to explore this feature. You can use any other tool of your choice which support Salesforce REST / SOAP callout.
URL for Workbench - https://workbench.developerforce.com/login.php
Callout and Test
I used below sample for the callout: -
/services/data/v58.0/query?q=SELECT+Name,+(SELECT+Name,Id,+(SELECT+Name,+Id,+(Select+Name,+Id,+(Select+Name,+Id+from+Level3__r)+from+Level2__r)+FROM+Level1__r)+FROM+Contacts)+FROM+Account
And result looked like below: -
Observation
totalSize
toatSize shows total number of records for the Parent object which is 101 Accounts in this exercise.
Maximum Result Size
Maximum Result Size in a SOQL Query is still 2000 and other limits still apply. 2000 records in this case is inclusive of all the records in all five levels in this case and that's why we see only 2 Account records, rest 1998 includes Contact, Level1__c, Level2__c and Level3__c records.
nextRecordsUrl
nextRecordsUrl parameter will return the next 2000 records and it also is inclusive of all the records in all five levels used in the SOQL.
Senior Tech Consultant at Salesforce ( 4x Certified )
11 个月Very Much helpful. Thanks!
Business Agility | Process Automation & Orchestration | Low Code Development | Financial Services
1 年Nice one Digamber Prasad