Wednesday, October 12, 2022

How to get list of all accounts present in account hierarchy in Salesforce

 As a developer, we always get requirement to display records in their hierarchy (Account Hierarchy, Case Hierarchy etc). Using SOQL queries, its really tough to get all accounts in hierarchy as we have to fire multiple SOQL queries to get ultimate parent record id if we are on a record which is 3 or 4 level down in hierarchy. 

Instead of achieving everything using SOQL, we can create formula field "Ultimate Account Id" in account object which will store ultimate account record id on all account records present in account hierarchy. As formula field allows you to refer upto 10 parent, we can easily get list of all account in account hierarchy upto 10 levels.


Below is formula for Ultimate Account Id.


Now if you know account record id, just get Ultimate Account Id field value and then query all accounts which have this ultimate account Id.

Below is static method which can be used to get list of all account present in account hierarchy.

public static list<Account> findAllHierarchyAccounts(string recordId){
list<Account> allAccountList=new List<Account>();
string ultimateAccountId;
for(Account acc:[select Ultimate_Account_Id__c from Account where Id=:recordId]){
ultimateAccountId=acc.Ultimate_Account_Id__c;
}
if(string.isNotBlank(ultimateAccountId)){
for(Account acc:[select id,Name,ParentId
from Account where Ultimate_Account_Id__c=:ultimateAccountId]){
allAccountList.add(acc);
}
}
system.debug('***allAccountList size:'+allAccountList);
return allAccountList;
}

Hope this will help!!


7 comments:

  1. Hi! Great writeup - super helpful. I'm a salesforce novice on the back end but if i wanted to pull the Ultimate Parent account name could i swap the Parent.Id fields with Parent.Name?

    ReplyDelete
  2. Hello Madam and Sir
    Your serious and honest loan offer
    The loan varies from 5,000 to 1,500,000 euros and the documents are available to us, please contact me by email: financialserviceoffer876@gmail.com whats-App +918929509036

    ReplyDelete
  3. Hello, This blog is very informative for every one, It's a Great think to help everyone thanks. Visit our website and find Great products in very cheap price https://alphatrade-ltd.com/

    ReplyDelete
  4. <I lost my job few months back and there was no way to get income for my family, things was so tough and I couldn’t get anything for my children, not until a met a recommendation on a page writing how Mr Bernie Wilfred helped a lady in getting a huge amount of profit every 6 working days on trading with his management on the cryptocurrency Market, to be honest I never believe it but I took the risk to take a loan of $2000. and I contacted him unbelievable and I was so happy I earn $22,500 in 6 working days, the most joy is that I can now take care of my family I don’t know how to appreciate your good work Mr. Bernie Doran God will continue to bless you for being a life saver I have no way to appreciate you than to tell people about your good services.
For a perfect investment and good strategies contact Mr Bernie Doran via Telegram : @Bernie_fx or Email : Bernie.doranfx01@gmail.com

    ReplyDelete
  5. Sound Engineering Colleges in India:

    Discover top-notch sound engineering education in India at Seamedu. Our world-class faculty and state-of-the-art facilities make us one of the leading sound engineering colleges, shaping future audio professionals. Sound Engineering Colleges in India

    ReplyDelete