+ Start a Discussion

Mimicking Account Hierarchy function almost working but still one problem to solve.

Salesforce Function "View Hierarchy" in the Account layout is very useful for displaying Account/ParentAccount relationship tree but sadly the displayed account field columns are not configurable. We built the following function which mimick the Salesforce hierarchy view performing "No. of hierarchy levels" +1 queries to retrieve the necessary data:

<script src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">

var level=1;
var space='        ';
var ac;
var accname="'" + "{!Account.Id}" + "'";

function first()//Retrieves data of highest level account
var ergo = sforce.connection.query("SELECT Account.Id, Account.Name, Account.Site, Account.Line_Name__c FROM Account WHERE Id =" + accname);  
rec = ergo.getArray("records");
document.write('<tr class="dataRow even"><th class="dataCell" scope="row"><a nowrap="nowrap" href="/' + rec[0].Id + '">' + rec[0].Name + '<\/a>
<\/th><td class="dataCell">' + rec[0].Site + '<\/td><td class="dataCell">' + rec[0].Line_Name__c + '<\/td><\/td><\/tr>'); function rekursiv(accid)//Runs once for each hierarchy level { var erg = sforce.connection.query("SELECT Account.Id, Account.Name, Account.Site, Account.Line_Name__c FROM Account WHERE ParentId IN (" + accid + ")"); records = erg.getArray("records"); ac=''; for (var j=0; j<level; j++) { space=space.concat(' '); } for(var i=0; i<records.length; i++) { var acc = records[i]; ac = ac.concat(",'" + acc.Id + "'"); document.write('<tr class="dataRow even"><th class="dataCell" scope="row">' + space + '<a nowrap="nowrap" href="/' + acc.Id + '">' + acc.Name + '<\/a>
<\/th><td class="dataCell">' + acc.Site + '<\/td><td class="dataCell">' + acc.Line_Name__c + '<\/td><\/tr>'); } if(ac!='')//If lower level Accounts found { level++; rekursiv(ac.slice(1)); } } rekursiv(accname); } </script>

However this function builds the following view

While it should be

It shows accounts grouped by their hierarchy level not by  their parent accounts and we have no idea how to correct it.
Is there a way to change the function to work properly?

Possibly it is necessary to pull an array of all Accounts in the hierarchy first, then sort them from each parent node down, and only afterwards generate output. But we failed to build a recursive function which sorts an array of accounts by their parent.

Message Edited by FGNY on 08-27-2008 02:32 AM
You have your recursion wrong.  If you want it to display hierarchically you have to recurse in the for loop.  That said, this sure looks chatty – you might think about using SOQL subqueries to reduce the number of queries each call to rekursiv does.
But how to make such in-loop recursion without performing a query for each Account (which would last too long)?

BTW Are "SOQL-subqueries" the same as nested Queries. I used them to retrieve Opportunity or Asset data within the same Accout query but a nested Account of Account query don't work.