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
Description

Object_With_Child_

Relationship_Name__mdt

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

Ex:

Parent Object
Child Relationship Name
Account

Contacts

Opportunities

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.

Screen

Select the object which you want to retrieve the records.

Screen

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.

Screen

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.

Screen

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

Screen

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

Screen

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

Screen

Limitations:

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

Note:

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

References:

View code in Github

1 thought on “SObject Tree Structure”

Leave a Comment

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

Recent Posts

top 5 benefits of using salesforce for high tech industry infographic
Top 5 Benefits of using Salesforce for High-Tech Industry
salesforce world tour essentials dubai
Salesforce World Tour Essentials Dubai | May 16, 2024
simplifying npl the magic of natural language processing
Simplifying NLP: The Magic of Natural Language Processing
streamlining-salesforce deployment with gearset a devops revolution Part 2
Streamlining Salesforce Deployment with Gearset: A DevOps Revolution (Part 2)
streamlining-salesforce deployment with gearset a devops revolution Part 1
Streamlining Salesforce Deployment with Gearset: A DevOps Revolution (Part1)
Scroll to Top