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
Narmadha Chandrasekar 4Narmadha Chandrasekar 4 

create a VF page to display Account Parent,child and grandchild names

create a VF page to display Account Parent,child and grandchild names like the screenshot below.Please helpUser-added image
Best Answer chosen by Narmadha Chandrasekar 4
Ajay K DubediAjay K Dubedi
Hi Bob,

You can use the below code:  

<<<<<<-----VF Page ------>>>>>
 
<apex:page controller="hierarchy" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection columns="1" title="Hierarchy For Account :: {!AccName}">
                <table style="border:1px solid black;width:100%;">
                    <tr>
                        <th>Parent Account</th>
                        <th>Child Account</th>
                        <th>Grand Child Account</th>
                    </tr>
                    <tr>
                        <td>{!wrapperObject.parentName}</td>
                        <td><table>
                            <apex:repeat value="{!wrapperObject.Wrapperchild}" var="item">
                                <tr>
                                    <td>{!item.childName}</td>
                                    
                                </tr>
                            </apex:repeat>
                            </table>
                            
                        </td>
                        <td>
                            <table>
                                <apex:repeat value="{!wrapperObject.Wrapperchild}" var="item">
                                <tr>
                                    <td>
                                        <table>
                                            <apex:repeat value="{!item.grandChildName}" var="item1">
                                                <tr>
                                                    <td>{!item1}</td>
                                                </tr>
                                            </apex:repeat>
                                        </table>
                                    </td>
                                </tr>
                            </apex:repeat>
                            </table>
                        </td>
                    </tr>
                </table>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>



<<<<<<<<<<------Controller---->>>>>>>>>
 
public with sharing class hierarchy{

    public id AccId {get;set;}
    public List<Account> Parent {get;set;}
    public String AccName {get;set;}
    public String AccParentName {get;set;}
    public List<Account> AccList {get;set;}
    public List<Account> AccChildList {get;set;}
    public List<Account> accGrandChildList {get;set;}
    public Wrapper wrapperObject {get;set;}
    
    public hierarchy(){
        wrapperObject = new Wrapper();
        List<Wrapperchild> childWrapperList = new List<Wrapperchild>();
        AccId = ApexPages.currentPage().getParameters().get('id');
        if(AccId != Null){
            Account Parent = new Account();
            AccList = [Select Name,parentid From Account WHERE Id = :AccId];
            AccChildList = [Select Name From Account WHERE ParentId = :AccId];
            Set<Id> accIds = new Set<Id>();
            for(Account acc : AccChildList){
                accIds.add(acc.Id);
            }
            accGrandChildList = [SELECT Name,ParentId FROM Account WHERE ParentId IN : accIds];
            if(AccList.size()>0){    
                AccName = AccList[0].Name;
                if(AccList[0].ParentId != Null){
                    Parent = [Select Name From Account Where Id = :AccList[0].ParentId Limit 1];
                    wrapperObject.parentName = Parent.Name;
                }
                for(Account accChild: AccChildList){
                    Wrapperchild wrapperChildObject = new Wrapperchild();
                    wrapperChildObject.childName = accChild.Name;
                    String name = '';
                    for(Account accGrandChild: accGrandChildList){
                        if(accChild.Id == accGrandChild.ParentId){
                            name = accGrandChild.Name;
                        }
                    }
                    wrapperChildObject.grandChildName.add(name);
                    childWrapperList.add(wrapperChildObject);
                }
                wrapperObject.Wrapperchild = childWrapperList ;
            }
        }        
    } 
    public class Wrapperchild{
        public String childName{get;set;}
        public List<String> grandChildName{get;set;}
        public Wrapperchild(){
            grandChildName = new List<String>();
        }
    } 
    public class Wrapper{
        public String parentName{get;set;}
        public List<Wrapperchild> Wrapperchild{get;set;}
    } 
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Narmadha,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

http://www.infallibletechie.com/2013/11/how-to-display-parent-child-and.html

http://salesforceblogger.blogspot.com/2012/03/hierarchy-in-visualforce.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Bob,

You can use the below code:  

<<<<<<-----VF Page ------>>>>>
 
<apex:page controller="hierarchy" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection columns="1" title="Hierarchy For Account :: {!AccName}">
                <table style="border:1px solid black;width:100%;">
                    <tr>
                        <th>Parent Account</th>
                        <th>Child Account</th>
                        <th>Grand Child Account</th>
                    </tr>
                    <tr>
                        <td>{!wrapperObject.parentName}</td>
                        <td><table>
                            <apex:repeat value="{!wrapperObject.Wrapperchild}" var="item">
                                <tr>
                                    <td>{!item.childName}</td>
                                    
                                </tr>
                            </apex:repeat>
                            </table>
                            
                        </td>
                        <td>
                            <table>
                                <apex:repeat value="{!wrapperObject.Wrapperchild}" var="item">
                                <tr>
                                    <td>
                                        <table>
                                            <apex:repeat value="{!item.grandChildName}" var="item1">
                                                <tr>
                                                    <td>{!item1}</td>
                                                </tr>
                                            </apex:repeat>
                                        </table>
                                    </td>
                                </tr>
                            </apex:repeat>
                            </table>
                        </td>
                    </tr>
                </table>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>



<<<<<<<<<<------Controller---->>>>>>>>>
 
public with sharing class hierarchy{

    public id AccId {get;set;}
    public List<Account> Parent {get;set;}
    public String AccName {get;set;}
    public String AccParentName {get;set;}
    public List<Account> AccList {get;set;}
    public List<Account> AccChildList {get;set;}
    public List<Account> accGrandChildList {get;set;}
    public Wrapper wrapperObject {get;set;}
    
    public hierarchy(){
        wrapperObject = new Wrapper();
        List<Wrapperchild> childWrapperList = new List<Wrapperchild>();
        AccId = ApexPages.currentPage().getParameters().get('id');
        if(AccId != Null){
            Account Parent = new Account();
            AccList = [Select Name,parentid From Account WHERE Id = :AccId];
            AccChildList = [Select Name From Account WHERE ParentId = :AccId];
            Set<Id> accIds = new Set<Id>();
            for(Account acc : AccChildList){
                accIds.add(acc.Id);
            }
            accGrandChildList = [SELECT Name,ParentId FROM Account WHERE ParentId IN : accIds];
            if(AccList.size()>0){    
                AccName = AccList[0].Name;
                if(AccList[0].ParentId != Null){
                    Parent = [Select Name From Account Where Id = :AccList[0].ParentId Limit 1];
                    wrapperObject.parentName = Parent.Name;
                }
                for(Account accChild: AccChildList){
                    Wrapperchild wrapperChildObject = new Wrapperchild();
                    wrapperChildObject.childName = accChild.Name;
                    String name = '';
                    for(Account accGrandChild: accGrandChildList){
                        if(accChild.Id == accGrandChild.ParentId){
                            name = accGrandChild.Name;
                        }
                    }
                    wrapperChildObject.grandChildName.add(name);
                    childWrapperList.add(wrapperChildObject);
                }
                wrapperObject.Wrapperchild = childWrapperList ;
            }
        }        
    } 
    public class Wrapperchild{
        public String childName{get;set;}
        public List<String> grandChildName{get;set;}
        public Wrapperchild(){
            grandChildName = new List<String>();
        }
    } 
    public class Wrapper{
        public String parentName{get;set;}
        public List<Wrapperchild> Wrapperchild{get;set;}
    } 
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
This was selected as the best answer