How to get all accounts from a hierarchy

Community Alums
Not applicable

Hi Team,

 

I have a requirement wherein I need to find all accounts under an hierarchy.Suppose I am on an account as highlighted in red below

Ankur20_0-1714059992915.png

I want to get the names of all the accounts in the hierarchy as below . Can someone tell me how to achieve it?

 

Ankur20_1-1714060034665.png

 

1 ACCEPTED SOLUTION

johnfeist
Mega Sage
Mega Sage

Hi Ankur,

 

The only way that I know of is to write a recursive script that starts at the top and queries for the next level of data.  For each entry at that level, the recursion keeps drilling down until it reaches the bottom.  There are several things to consider when setting up something like this.  The most important one is where are you holding the data as you move between levels, etc.  If you are not familiar with recursive programming, it can take a little getting used to.

 

Here is a simplistic version of a recursive function:

 

digDown : function(whatAccount, theData) {
   var myTable = new GlideRecord("<your table name>);
   myTable.addQuery("<account field>", whatAccount);
   myTable.query();
   while (myTable.next()) {
      digDown(myTable.sys_id.toString, theData)
   } else {
      theData.push(myTavle.<identifier>.getDisplayValue());
      return;
   }
}

 

theData is an empty array that gets created before invoking the function.  What will happen is that when it gets to the bottom of one leg of the hierarchy it will add that value to the array and go back to the next higher level where it will continue going through the rows selected until all have been done and them go back up a level.  Of course you have some ohter options around how you store the data based on what else you need, etc.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

1 REPLY 1

johnfeist
Mega Sage
Mega Sage

Hi Ankur,

 

The only way that I know of is to write a recursive script that starts at the top and queries for the next level of data.  For each entry at that level, the recursion keeps drilling down until it reaches the bottom.  There are several things to consider when setting up something like this.  The most important one is where are you holding the data as you move between levels, etc.  If you are not familiar with recursive programming, it can take a little getting used to.

 

Here is a simplistic version of a recursive function:

 

digDown : function(whatAccount, theData) {
   var myTable = new GlideRecord("<your table name>);
   myTable.addQuery("<account field>", whatAccount);
   myTable.query();
   while (myTable.next()) {
      digDown(myTable.sys_id.toString, theData)
   } else {
      theData.push(myTavle.<identifier>.getDisplayValue());
      return;
   }
}

 

theData is an empty array that gets created before invoking the function.  What will happen is that when it gets to the bottom of one leg of the hierarchy it will add that value to the array and go back to the next higher level where it will continue going through the rows selected until all have been done and them go back up a level.  Of course you have some ohter options around how you store the data based on what else you need, etc.

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster