Gem of Salesforce Summer'23 Release - Query Five Levels of Parent-to-Child Relationships - Know How
Salesforce Developers - Know How

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.

No alt text provided for this image
Five Level Parent-to-Child Relationship Object Model

Data Load

For this exercise, I loaded a few thousands of records using data loader. Summary of data volume: -

No alt text provided for this image
Data Volume Summary

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: -

No alt text provided for this image
Callout Result

Observation

totalSize

toatSize shows total number of records for the Parent object which is 101 Accounts in this exercise.

No alt text provided for this image
totalSize

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.

No alt text provided for this image


Shridevi Badiger

Senior Tech Consultant at Salesforce ( 4x Certified )

11 个月

Very Much helpful. Thanks!

Chris Mooney

Business Agility | Process Automation & Orchestration | Low Code Development | Financial Services

1 年

Nice one Digamber Prasad

要查看或添加评论,请登录

Digamber Prasad的更多文章

社区洞察

其他会员也浏览了