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 

Not finding a proper solution for account hierarachy

I have a requirement on which on click of the button i have to show the Hierarchy of accounts & its related contacts like :
Account1->Contact1,Contact2
Account2->Contact1,Contact2
Not finding a proper code to get this data in another VF Page.
Martijn SchwarzerMartijn Schwarzer
Hi Deepika,

I've created a simple vf page for you that will do the trick. You will have to adjust it to your needs, but the basics are there.

VF Page:
 
<apex:page controller="AccountContactHierarchy">
    <apex:form>
    	<apex:pageBlock title="Accounts and Contacts">
            <table border="1">
                <tr>
                	<th>Account Name</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>City</th>
                    <th>State</th>
                </tr>
                <apex:repeat value="{!accounts}" var="a">
                    <tr>
                        <td>{!a.Name}</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <apex:repeat value="{!a.Contacts}" var="c">
                        <tr>
                            <td>&nbsp;</td>
                            <td>{!c.FirstName}</td>
                            <td>{!c.LastName}</td>
                            <td>{!c.MailingCity}</td>
                            <td>{!c.MailingState}</td>
                        </tr>
                    </apex:repeat>
                </apex:repeat>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And the controller:
 
public class AccountContactHierarchy {
    public List<Account> accounts {get; private set;}
    
    public AccountContactHierarchy(){
        this.accounts = [Select Id, Name, (Select Id, FirstName, LastName, MailingCity, MailingState from Contacts) From Account LIMIT 10];
	}
}

This will output the following table:

User-added image

Hope this helps!

Best regards,
Martijn Schwärzer
JyothsnaJyothsna (Salesforce Developers) 
Hi Deepika,

Please try the below sample code.

Visualforce Page
 
<apex:page controller="LookupHierarchy">
  <apex:form >
  <apex:pageBlock >
  <apex:pageBlockTable value="{!Account}" var="p">
  <apex:column headerValue="Parent name" value="{!p.name}"/>
  <apex:column >
  <apex:pageBlockTable value="{!p.contacts}" var="c">
  <apex:column value="{!c.Lastname}"  headerValue="Child Names"/>
  <apex:column value="{!c.phone}" />
  </apex:pageBlockTable>
  </apex:column>
  </apex:pageBlockTable>
 
 
  </apex:pageBlock>
  </apex:form>
  
</apex:page>

Controller
 
public with sharing class LookupHierarchy {

    public List<account> Account { get; set; }
    
    public List<contact> child { get; set; }

    
    public LookupHierarchy (){
       account =new List<account>();
       account=[select name,(select Lastname,phone from Contacts) from account];
       
       
    }
}

Output

User-added image

Hope this helps you!
Best Regards,
Jyothsna
Deepika Gupta 26Deepika Gupta 26
Thanks for the solution.but i want it in a hierarchy form like Account and if we click on +button it will expand and will show all the accounts and if expand any one account by + button it will show me its related contact.
Martijn SchwarzerMartijn Schwarzer
Hi Deepika,

You can do that using the Lighting Design System Tree component. You can find the information here:

https://www.lightningdesignsystem.com/components/trees/

Happy coding!

Best regards,
Martijn Schwärzer