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|
|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|
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.When 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.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