function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepika Gupta 26Deepika Gupta 26 

Account Hierarchy Contact Association

Hi,as per my req able to print hierarchy on vf page with parent and its corresponding child account
/**
 * @description This is the controller class for HierarchyTree component
 * @author Valentyn Bilenkyi
 * @date 10/23/2015
 */
public without sharing class HierarchyTree {

    public static String currentId {get; set;}

    private static Account currentAccount {
        get {
            if (currentAccount == null) {
                currentAccount = [select Name, ParentId, UltimateParent__c From Account where Id = :currentId];
            }
            return currentAccount;
        }
        private set;
    }

    private static Account ultimateParentAccount {
        get {
            if (ultimateParentAccount == null) {
                if (currentAccount.UltimateParent__c != null) {
                    ultimateParentAccount = [select Name, ParentId, UltimateParent__c from Account where Id = :currentAccount.UltimateParent__c];
                } else {
                    ultimateParentAccount = currentAccount;
                }
               
            }
            return ultimateParentAccount;
        }
        private set;
    }
    
    /**
     * @description This methos is used to generate JSON data for jsTree component
     */
    public static String getJsonTree() {
        List<JSTreeNode> listItems = new List<JSTreeNode>();
        listItems.add(
            new JSTreeNode(
                ultimateParentAccount.Id,
                '#',
                ultimateParentAccount.Name,
                new JSTreeNodeState(true, false, false),
                new JSTreeNodeAAttr(String.valueOf(ultimateParentAccount.Id) == currentId ? 'font-weight:bold' : '')
                
            )
        );
        for (Account acc : AccountHierarchyServices.getAllChildren(new List<Account>{ultimateParentAccount})) {
            listItems.add(
                new JSTreeNode(
                    acc.Id,
                    acc.ParentId,
                    acc.Name,
                    new JSTreeNodeState(true, false, false),
                    new JSTreeNodeAAttr(String.valueOf(acc.Id) == currentId ? 'font-weight:bold' : '')
                )
            );
        }
        return JSON.serialize(listItems);
    }

    /**
     * This class is used to generate jsTree node
     */
    @TestVisible private class JSTreeNode {
        public String id; //required
        public String parent; //required
        public String text; // node text
        public String icon; // string for custom icon
        public JSTreeNodeState state; // instance of JSTreeNodeState
        public Object li_attr; // attributes for the generated LI node
        public JSTreeNodeAAttr a_attr; // attributes for the generated A node
        
        public JSTreeNode(String id, String parent, String text, JSTreeNodeState state, JSTreeNodeAAttr a_attr) {
            this.id = id;
            this.parent = parent;
            this.text = text;
            this.state = state;
            this.a_attr = a_attr;
        }
    }

    @TestVisible private class JSTreeNodeState {
        public Boolean opened; // is the node open
        public Boolean disabled; // is the node disabled
        public Boolean selected; // is the node selected

        public JSTreeNodeState(Boolean opened, Boolean disabled, Boolean selected) {
            this.opened = opened;
            this.disabled = disabled;
            this.selected = selected;
        }
    }

    @TestVisible private class JSTreeNodeAAttr {
        public String style; // style of current node

        public JSTreeNodeAAttr(String style) {
            this.style = style;
        }
    }
    
}

Now i have to associate corresponding contacts to that particular account on the hierarchy.I am clear with the query like :
[select Name, ParentId, UltimateParent__c, (SELECT Id, Name FROM Contacts) From Account where Id = :currentId]; But i am not sure how can use this query to in this particular code to get a desire result like account->account1->contact1,Contact2.Plesae help me out if any body is aware of this hierarchy concept.