Apex · Lightning · Salesforce

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 Shot 2017-03-02 at 12.48.53 PM.png

Select the object which you want to retrieve the records.
Screen Shot 2017-03-02 at 12.49.03 PM.png

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.Screen Shot 2017-03-08 at 11.40.34 PM.pngWhen 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 Shot 2017-03-06 at 11.24.26 PM.png

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 Shot 2017-03-06 at 11.25.01 PM.png

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

Screen Shot 2017-03-06 at 11.25.58 PM.png

In the below code i am checking if the relationship name is present in Custom Metadata or not.Screen Shot 2017-03-08 at 11.47.05 PM.pngWe don’t know how many levels it goes so I am creating a component dynamically to display records in page.Screen Shot 2017-03-08 at 11.48.54 PM.pngLimitations:

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.

ReferencesView code in Github

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s