Aug 30, 2016

Dynamic nth Level Hierarchy in Salesforce

This blog is somewhat related to my previous post but with bit easy implementation and more generic. We have encountered this implementation multiple time so thought of sharing it with all.

Ever thought of replicating this feature in any of your custom object?

Now the only complication is, in case of self lookup you don't know the end node and where your loop should stop (to think about the logic). So hope I will make it easy for you.

*Note : My blog code formatter is not working properly, so please check the spaces before using the code.

Step 1 : Create an apex class called HierarchyController, as shown below and save it.

public with sharing class DynamicHierarchyForBlogController 
 public String objectApiName {get;set;}
 public String fieldApiName {get;set;}
 public String nameField {get;set;}
 public String currentsObjectId {get;set;}
 public String topParentId {get;set;}
 public String jsonMapsObjectString {get;set;}
 private String jsonString;
 private Map> sObjectIdMap ;
 private Map selectedsObjectMap ;
 private Map allsObjectMap;

 public DynamicHierarchyForBlogController() 
  currentsObjectId = Apexpages.currentPage().getParameters().get('id');
     sObjectIdMap = new Map>();
  selectedsObjectMap = new Map();
  allsObjectMap = new Map();
 public String getjsonMapString()
  return jsonString;
 public void retrieveInfo()
  String dynamicQuery = 'SELECT ID ,' + fieldApiName + ' , ' + nameField + ' FROM ' + objectApiName + ' ORDER BY ' + fieldApiName + '  LIMIT 50000';
  for(sObject obj: Database.query(dynamicQuery))

  if(currentsObjectId != null)
   String dQuery = 'SELECT ID FROM ' + objectApiName + ' WHERE id =\'' + currentsObjectId +'\'';
   List objList = Database.query(dQuery);
   currentsObjectId = objList[0].Id;
   retrieveAllChildRecords(new Set{topParentId});
   for(String str : sObjectIdMap.keySet())
   jsonString = JSON.serialize(sObjectIdMap);
   jsonMapsObjectString = JSON.serialize(selectedsObjectMap);

 public void retrieveTopParent(String sObjectId)
  if(allsObjectMap.keySet().contains(sObjectId) && allsObjectMap.get(sObjectId).get(fieldApiName) != null)
   topParentId = String.valueOf(allsObjectMap.get(sObjectId).get(fieldApiName));

 public void retrieveAllChildRecords(Set sObjectIdSet)
  if(sObjectIdSet.size() > 0)
   Set allChildsIdSet = new Set();
   for(String str : sObjectIdSet)
    Set childsObjectIdSet = new Set();
    for(sObject obj : allsObjectMap.values())
     if(obj.get(fieldApiName) != null && String.valueOf(obj.get(fieldApiName)) == str)

Step 2 : Now create a component called Hierarchy, as shown below and save it.

<apex:component controller="DynamicHierarchyForBlogController">
 <apex:attribute name="objectName" description="Name of Object." type="String" required="true" assignTo="{!objectApiName}"/>
 <apex:attribute name="FieldName" description="Name of the field of the object." type="String" required="true" assignTo="{!fieldApiName}"/>
 <apex:attribute name="RepersenterField" description="Name field of the object." type="String" required="true" assignTo="{!nameField}"/>
 <script src=""></script>
 <div id="parentDiv"></div>
     #parentDiv ul:first-child
      padding: 0;
     #parentDiv li
      list-style: none;
      padding: 10px 5px 0 5px;
      position: relative;
  #parentDiv li span 
      -moz-border-radius: 5px;
      -webkit-border-radius: 5px;
      /*border: 1px solid #999;*/
      border-radius: 5px;
      display: inline-block;
      padding: 3px 8px;
      cursor: pointer;
      var accountMap = JSON.parse('{!jsonMapString}');
      var accountValueMap = JSON.parse('{!jsonMapsobjectString}');
      var cClass = '';
  if("{!topParentId}".indexOf('{!$}') != -1)
   cClass = 'selectedRecord';
  var ul = '<ul><li id="{!topParentId}"  ><span class="' + cClass + '"  onclick="toggleChilds(\'' + '{!topParentId}' + '\',event)" ><b id="i{!topParentId}" class="minus" style="font-size: 1.5em;" >-</b>&nbsp;' +  accountValueMap['{!topParentId}'].{!RepersenterField} + '</span></li></ul>'  ;
      function appendUl(key)
       $.each( accountMap[key], function( index, value ) 
      var dclass = '';
      if(value.indexOf('{!$}') != -1)
       dclass = 'selectedRecord';
      var ul = '<ul class="' + key + '"><li id="' + value + '" ><span class="' + dclass + '" onclick="toggleChilds(\'' + value + '\',event)" ><b id="i' + value + '" class="minus" style="font-size: 1.5em;" >-</b>&nbsp;' + accountValueMap[value].{!RepersenterField} + "</span></li></ul>"  ;
      $(ul).appendTo("#" + key);
      function toggleChilds(key,event)

Step 3 : Now create a visualforce page, as shown below.

<apex:page standardcontroller="Account" showHeader="true" sidebar="true">
 <apex:pageblock title="Hierarchy">
  <c:DynamicHierarchyForBlog objectName="Account" FieldName="ParentId" RepersenterField="Name"/>
Open the page in the browser and pass a valid account id and the output will be a collapsible hierarchy as shown below.

This can plot the hierarchy of nth level and of any object. Post as comment if you have any questions.

Happy Coding!!


  1. Very informative blog and very useful article thank you for sharing with us , keep posting learn more about salesforce training,salesforce online training

  2. This is very informative blog and useful article thank you for sharing with us keep posting more details aboutsalesforce training in bangalore,salesforce online training

  3. This is very informative blog and nice article , I really like your technique of writing a blog. I book marked it to my bookmark site list and will be checking back in the near future.Salesforce training, salesforce online training

  4. Nice information about dynamic nth Level hierarchy My sincere thanks for sharing this post and please continue to share this kind of post
    Salesfoce Training in Chennai

  5. Thanks for sharing this. This is really a very informative blog. Nice information regarding salesforce implementation services .This is very helpful for those who want to become a saleforce developer. Keep Posting.

  6. Nice blog..! I really loved reading through this article... Thanks for sharing such an amazing post with us and keep blogging...
    ios app development course
    iphone app training course in bangalore

  7. Nice blog..! I really loved reading through this article... Thanks for sharing such an amazing post with us and keep blogging...
    sap consulting services in usa

  8. Are you looking for Best Cloud Computing training in Delhi. DIAC offering best online Salesforce training , CRM training, Salesforce Lightning - Admin developer training. Free Demo Class. Call now 9310096831.

  9. Are you looking for Best Cloud Computing training in Delhi. DIAC offering best online Salesforce training , CRM training, Salesforce Lightning - Admin developer training. Free Demo Class. Call now 9310096831.

  10. I really enjoy the blog.Much thanks again. Really Great salesforce Online course

  11. hi, nice information is given in this blog. Thanks for sharing this type of information, it is so useful for me. nice work keep it up.
    Travel visa provider in Delhi


  12. Awesome blog. It was very informative. I would like to appreciate you. Keep updated like this best sap simple finance online training institute in hyderabad

  13. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

  14. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    advanced java training in coimbatore
    best java training in coimbatore

  15. All the latest updates from the PythonAutomationminds team. Python Automationminds lets you program in Python, in your browser. No need to install any software, just start coding straight away. There's a fully-functional web-based console and a programmer's text-editor
    Phyton training in Chennai

  16. This content is very good way for devolep the knowledge. Thanks for your wonderful post. I would like to regular updates from your blog...
    Best IELTS coaching in Chennai
    IELTS classes in Chennai
    Best IELTS coaching centres in Chennai
    IELTS coaching center in Chennai

  17. Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.

    Article submission sites

  18. Having an assignment due tomorrow and understand you can’t do it yourself? But you’re afraid of delegating your paper to some scam services as well? Why not check right now?

  19. Very nice your post and tips sharing thanks forceguru

  20. Very nice your post and tips sharing thanks forceguru
    SEO Training in Chennai

  21. Good blog informative
    Sanjary Academy provides excellent training for Piping design course. Best Piping Design Training Institute in Hyderabad, Telangana. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller course,pressure Vessel Design Course, Welding Inspector Course, Quality Management Course, #Safety officer course.
    Piping Design Course in India­