Hi All,
I am writing this post because 2-3 clients came up with this same requirement, to show all the contacts of child Accounts in their parent Account.
I am explaining this requirement in detail with an example:-
Suppose there is Account "A" who is parent of Account "B".
now account "B" has 2 contacts "B1" and "B2".
client want to see this "B1" & "B2" contacts in parent account "A", so that he can easily know which contact associated with which account on same page easily.
And this is not only limited to 2 level it is goes to up to 100 level (we can't Query more then 100 in one context because of Governor limit).
I solved this problem by creating a VF page and add it to Detail page of Account, So now user can able to see all the contacts associated with the Account and their child Accounts in a same page.
Here the code:-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page standardController="Account" extensions="ChildAccountsContactsController"> | |
<apex:pageblock> | |
<apex:pageBlockSection title="All Contacs including child Accounts" columns="1"> | |
<apex:pageBlockTable value="{!allContactNames}" var="con"> | |
<apex:column value="{!con.Name}" headerValue="Contact Name"/> | |
<apex:column value="{!con.Account.Name}" headerValue="Contact's Account Name"/> | |
<apex:column value="{!con.Account.Parent.Name}" headerValue="Contact's Parent Account Name"/> | |
</apex:pageBlockTable> | |
</apex:pageBlockSection> | |
</apex:pageblock> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChildAccountsContactsController{ | |
public List<Contact> allContactNames{get;set;} | |
public ChildAccountsContactsController.ChildAccountsContactsController(ApexPages.StandardController controller){ | |
Id accId = controller.getId(); | |
Set<Id> allChildAccount = getAllChildAccountIds(accId); | |
allContactNames = new List<Contact>(); | |
allContactNames = [Select Id, Name, Account.Name, Account.Parent.Name from Contact where AccountId in :allChildAccount]; | |
} | |
private Set<Id> getAllChildAccountIds(Id parentAccountId){ | |
Set<Id> AccountIdSet = new Set<Id>(); | |
AccountIdSet.add(parentAccountId); | |
Boolean check=true; | |
do | |
{ | |
List<Account> accIds = [select Id from Account where ParentId in:AccountIdSet And Id not in:AccountIdSet]; | |
if(accIds != null && accIds.size() > 0){ | |
for(Integer i=0; i<accIds.size(); i++){ | |
AccountIdSet.add(accIds[i].Id); | |
} | |
} | |
else | |
check = false; | |
}while(check); | |
return AccountIdSet; | |
} | |
} |
Screen Shots:-
Happy Coding...:)
Thanks
Anurag Jain
Great post! By the way what is the necessity for do-while loop in getAllChildAccountIds() method?
ReplyDeleteHi Surendra,
Deletesorry for late reply, Doug answered your query but if still do you have any doubt you can ask me??
Thanks
Anurag
It loops through the account child hierarchy until there are no more child accounts found. The first pass grabs 1st level children. Next pass grabs their children, etc.
ReplyDeleteYou either have to do recursive call or loop and requery like this. One gotcha is beware governor limit of max SOQL queries since a query occurs within loop, which author mentions on max depth this solution can support.
thanks Doug for Answering Surendranadh question.
Delete
ReplyDeletethis blog is really useful and it is very interesting thanks for sharing , it is really good and exclusive.
php Training in Chennai
thanks deeksha...
DeleteHi Anurag,
ReplyDeleteis there a smart way to display parent account contacts on child account level?
This is what we are currently looking for.
Kind Regards,
Andy
Hi Andy,
DeleteYou may be interested in new feature in Summer '16 release that allows you to associate a contact with multiple accounts: https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_sales_shared_contacts.htm
You could use this so associate the parent account contacts with the sub accounts via indirect relationship. Likewise with contacts of child accounts be related indirectly with parent accounts. In this manner you would be able to see all the contacts on the related lists.
Another approach would be to write a visualforce page that queries for contacts at both the parent and child account level and display this custom page on the Account page layout: https://developer.salesforce.com/docs/atlas.en-us.workbook_vf.meta/workbook_vf/overrides_2.htm
@DouglasCAyers