It is very common use case to display hierarchy relation between records in tabular form (for example account hierarchy, case hierarchy etc.) In order to achieve this, I have created a reusable visualforce component for this purpose which can be customize as per requirement to display hierarchy in table tree grid.
Below are inputs required for component:
For example to generate the account hierarchy, create a VF page with code :
<apex:page controller="hierarchyComponentController">
<c:hierarchyComponent sObjectAPIName="Account" sParentfieldAPIName="ParentId" ColumnsToDispaly="Name, Type, Industry" RecordLinkfieldAPIName="Name" ColumnsLabels="Account Name, Type, Industry"/>
</apex:page>
Note: Don't forget to append record Id on visualforce page url for which you want to display hierarchy. Below is sample URL:
https://xxxxxxxxx.visual.force.com/apex/accHierarchy?id=0019000001NqoXZ
Hope this will help...
This can be used as base code and can be customize as per your requirements.
Looking forward for everyone's comment and feedback.
Below are inputs required for component:
- Specify the object name for which you want to display hierarchy.
- Specify the parent field API name (used for self relationship).
- Specify the API names of fields separated by comma which you want to display in table tree grid.
- Specify the columns labels separated by comma (make sure sequence is same as that of API fields name )for table tree grid.
- Specify the field API name which will display as hyperlink for record detail page.
You can create new VF page and include this component and specify above inputs and component will display the hierarchy of record starting from top most parent.
Below is visualforce component and apex class code:
For example to generate the account hierarchy, create a VF page with code :
<apex:page controller="hierarchyComponentController">
<c:hierarchyComponent sObjectAPIName="Account" sParentfieldAPIName="ParentId" ColumnsToDispaly="Name, Type, Industry" RecordLinkfieldAPIName="Name" ColumnsLabels="Account Name, Type, Industry"/>
</apex:page>
Note: Don't forget to append record Id on visualforce page url for which you want to display hierarchy. Below is sample URL:
https://xxxxxxxxx.visual.force.com/apex/accHierarchy?id=0019000001NqoXZ
Hope this will help...
This can be used as base code and can be customize as per your requirements.
Looking forward for everyone's comment and feedback.
More Blogs>>: Custom Metadata Types and Custom Settings Implementation Tricks
Visualforce Component for Record Status Progress Bar
Dynamic Apex in Salesforce
Refer Below Links for Salesforce Interview Questions
Sunil, if we want to do for 5 level hierarchy, will this work ?
ReplyDeleteYes. This will work. I have tested upto 10 level deep.
DeleteHi sunil,
ReplyDeleteThank you very much for the code,it really helpful for me,but when i try it in my org i am getting this error
Error: is required and must be the outermost tag in the markup at line 1 column 1
where is c:hierarchyComponent code?can you please suggest me
Hi sunil,
DeleteI got the solution,I have done dumb mistake.
Thanks
Hi Sunil,
ReplyDeleteInstead of passing single id in url,I want to display all records of object and when i click a record it should display hirarchy of that record,Can we do it with the code you provided.Can you please help in this.Its urgent
Thanks,
nandeesh.
I sunil thanks for your blog. I want to display parent releted child till nth level but its showing all the children at the third level .
ReplyDeleteCan you please help me on that.
What would it take to make this display in a Lightning Component?
ReplyDeleteHi Thaddeus,
DeleteYou can refer below blog to implement this in Lightning.
http://www.sfdcstuff.com/2017/10/lightning-tree-generic-lightning.html
If you think your girlfriend is cheating on you, then this is the e-book for you. Learn all of the techniques to catch her at her own game lie detectors
ReplyDeleteThese are common but important things we live by each day and we cannot just simply ignore that our partners may have interests different to that of our own. Dealing with it is never easy especially if you are not use to it. what to talk to girls about
ReplyDeleteKnows more about relationships? Then give a helping hand to your young ones. Let's grow the world knowledge. Head over to Thinkle Reletionship and help a seeker please.
ReplyDeleteI'm having an issue where child records are appending at the bottom and not nested under the parent. I have top tier, company, & company branch hierarchy. Companies are accurately listed under Top Tier, but the branches are not listed under their appropriate company.
ReplyDeletei love reading this article so beautiful!!great job! Relatietherapie Zaandam
ReplyDeleteThis is awesome, but the spacing is all off. Can anyone help me with this?
ReplyDelete