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
Enea GjokaEnea Gjoka 

Show Parent Account's Contacts on Child Accounts Contact related list?

Hi all,
I want to show in child accounts, the related contacts of the parents accounts too. I almost solved this with a Visualforce Page and a Controller, but the related contacts are shown only on the parent account and the current account contacts. 
How do I show the related contacts of the other parent child accounts contacts on the child account too?

Example: I have parent account A, and child Account B and C. When I click on the Account C, i want the visual force page to show me the related contacts of the Account A + the related contacts of account B. 

Thanks in advance for the help.

Here is my Controller:

public with sharing class AccountHierarchyController {
    public Account acc { get; private set; }
    public List<Account> accountList { get; private set; }
    public ApexPages.StandardController controller { get; private set; }
    
    public AccountHierarchyController(ApexPages.StandardController controller) {
        this.acc = (Account)controller.getRecord();
        this.accountList = new List<Account>();
        
        Account baseAccount = [SELECT Id, 
                               ParentId, 
                               Name,
                               Parent.Name,
                               (SELECT Id, Name, Phone, Email, Title from Contacts),
                               (SELECT Id, Name FROM ChildAccounts)
                               FROM Account 
                               WHERE Id = :acc.id];
        accountList.add(baseAccount);
        
        Account currentAccount = baseAccount;
 
        // Traverse the hierarchy upwards.
        while (currentAccount.ParentId != null) {
            currentAccount = [Select id, Name, ParentId, Parent.Name, (Select Id, Name, Phone, Email, Title from Contacts) from Account where Id = :currentAccount.ParentId];
            accountList.add(currentAccount);
        }
        
        // Traverse the hierarchy downwards
        Set<Id> accountsToQuery = new Map<Id, Account>(baseAccount.ChildAccounts).keySet();
        while (accountsToQuery.size() > 0) {
            List<Account> thisLevelAccounts = [SELECT Id, Name, Parent.Name,
                                               (Select Id, Name, Phone, Email, Title  from Contacts),
                                               (SELECT Id, Name FROM ChildAccounts)
                                               FROM Account
                                               WHERE Id IN :accountsToQuery];
            accountsToQuery = new Set<Id>();
            
            for (Account a : thisLevelAccounts) {
                // Add this Account (with its Contacts) to the list.
                accountList.add(a);
                
                // Add this Account's children to the query for the next level.
                for (Account child : a.ChildAccounts) {
                    accountsToQuery.add(child.Id);
                }
            }
        }
    }
}


Here is the Visual force page:

<apex:page title="Contact" standardController="Account" extensions="AccountHierarchyController" lightningStylesheets="true">
    <apex:outputPanel id="cont">
        <apex:pageBlock title="Contacts">
            <apex:repeat value="{! accountList }" var="a">
                <apex:pageBlockSection title="{! a.Name + IF(NOT(ISBLANK(a.ParentId)), ' (child of ' + a.Parent.Name + ')', '') }">
                    <apex:pageBlockTable value="{! a.Contacts }" var="con" id="conlist" title="Contact">
                        <apex:column value="{!con.Name}"/>
                        <apex:column value="{!con.Phone}" />
                        <apex:column value="{!con.Email}" />
                        <apex:column value="{!con.Title}" />
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:repeat>
        </apex:pageBlock>
    </apex:outputPanel>
</apex:page>
ShirishaShirisha (Salesforce Developers) 
Hi Enea,

Greetings!

It is difficult to troubleshoot the huge code which you have provided.However,I would suggest you to check the below document which might help in troubleshooting it from your end since you have an implementation idea of your own code.

https://developer.salesforce.com/forums/?id=906F00000008sS6IAI

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri