SObject Tree Structure

SObject Tree Structure

In this article, we are implementing a Tree Structure which displays all the parent and its respective child and grandchild records. When clicked on certain parent record, its respective child records are shown and when we click on child records they respective grandchild records are displayed. This goes on. The current implementation is generic which works on all objects. Select an object initially, then the records of the object are displayed in the Tree Structure.

Create a custom metadata object

Custom Metadata:

Used to display child records based on metadata records.

Custom Metadata API Name
Custom Fields API Name



It should have Child Relationship Name.
It should have parent object API Name
Custom Metadata


Parent Object
Child Relationship Name



Create a metadata with which objects you want to display to get the records from and their child objects. Those objects will be displayed in the picklist.


Select the object which you want to retrieve the records.


In the below code i am trying to get the SObject API Names by using schema and checking with Metadata, if API Name of the SObject present in Metadata it will display in picklist field.ScreenWhen you select a particular object and click on Get Records button you will get the particular selected object record.

If you have a self-relationship it will display only records which are not self-related records.

Ex: Let say we have Account1, Account2, Account3, Account4 records in account object.

For Account3 and Account4, Account1 is the parent.

So when you click on Get Records button it will display Account1 and Account2 but not Account3 and Account4 because Account3 and Account4 are the child records.


If you want to get the child records which are related to particular parent record you need to click on the arrow button. It will display all object records which are defined in metadata only.


If your parent record doesn’t have any child records it will a message.


In the below code i am checking if the relationship name is present in Custom Metadata or not.


We don’t know how many levels it goes so I am creating a component dynamically to display records in page.



As per the salesforce governor limits, we can able to retrieve 20 child relationships object records from SOQL.

For some the objects, it won’t be supported for Notes&Attachments


In custom metadata, you should have only 20 child relationships per parent object.


View code in Github

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

salesforce for financial services transforming customer engagement operational effectiveness
Salesforce for Financial Services: Transforming Customer Engagement & Operational Effectiveness
einstein copilot transform your salesforce experience
How Einstein Copilot Actions Can Transform Your Salesforce Experience
how can salesforce experience cloud drive customer engagement rates
How Can Salesforce Experience Cloud Drive Customer Engagement Rates?
salesforce world tour essentials mumbai
Salesforce World Tour Essentials Mumbai (19 June, 2024)
benefits of using salesforce for higher education Industry
Benefits Of Using Salesforce For Higher Education Industry?
Scroll to Top