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
Sam MohyeeSam Mohyee 

List all accounts below parent account in hierarchy

It's easy enough to write a SOQL query to pull all the immediate child accounts of a given account. But what if I want the children's children, etc, all the way to the 'leaf' accounts (the children that have no children)?

 

Is there an existing method provided by Salesforce that would return this when given an Account ID?

 

If not, I figure recursion is the way to go, assuming Salesforce permits it. Something along the lines of this pseudocode

 

list_build_function (account ID){

   Query all children accounts;

   Put IDs in child_ID_lis

   Add IDs onto full_ID_list;

   for (each ID in child_ID_list){
         list_build_function(child ID);
}
}

 

 

 

testrest97testrest97

Yep, i guess thats the only way, you cannot go more than one level deep in soql query