Tuesday, 9 February 2016

Show all contacts from Child Accounts in parents account.






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:-

Screen Shots:-


 Happy Coding...:)
Thanks
Anurag Jain

8 comments:

  1. Great post! By the way what is the necessity for do-while loop in getAllChildAccountIds() method?

    ReplyDelete
    Replies
    1. Hi Surendra,

      sorry for late reply, Doug answered your query but if still do you have any doubt you can ask me??

      Thanks
      Anurag

      Delete
  2. 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.

    You 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.

    ReplyDelete
    Replies
    1. thanks Doug for Answering Surendranadh question.

      Delete

  3. this blog is really useful and it is very interesting thanks for sharing , it is really good and exclusive.

    php Training in Chennai

    ReplyDelete
  4. Hi Anurag,

    is there a smart way to display parent account contacts on child account level?
    This is what we are currently looking for.

    Kind Regards,
    Andy

    ReplyDelete
    Replies
    1. Hi Andy,

      You 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

      Delete