You need to sign in to do that
Don't have an account?

account hierachy
I have a usecase ,where i have to traverse through account hierachy and if i find an account with a speical characteristic need to fetch it,else get the top most account in hierarchy.
Limit for level of hierarchy is infinite.
I tried coding this(am new to Apex :( ),by a recursive kind of function to avoid SOQL hitting.Not sure how efficient below code is.
But i couldnt find an exit kind of function to exit from the recursive calls to get back to trigger in APEX.Is there one?
below is my code.review comments are welcom :)
Code as such works,but when i try to test this against large datasets,its fine for accounts when less than 1000 in numbers,when after 1000 it gives me too many script statement errors,is there a maximum number to test against in testclass,althoguh everywhere it says just 200
Before Trigger on Account
-----------------------
trigger GEN_BeforeInsertUpdate on Account (before insert,before update) {
Account VGEorTopParent = new account();
testclass obj = new testclass();
integer queryLimit = Limits.getLimitQueryRows()-Limits.getQueryRows();
List<account> existingAccounts = [select parentID,Is_VGE_Account__c from Account limit :queryLimit ];
for (Account currAcc :Trigger.new)
{
//Checking whether current Account has a parent.Only if it has a parent execute all logic
if (currAcc.ParentId != null)
{
// for(List<Account> acclist :existingAccounts)
// {
Map<ID, Account> parentMap = new Map<ID, Account>(existingAccounts);
//Looping through map values
if (!parentMap.isempty())
{
for (Account acc:parentMap.values())
{
if (acc.ID == currAcc.parentID)
{
VGEorTopParent = obj.getnextparent(acc, parentMap);
currAcc.Global_Ultimate__c = VGEorTopParent.ID;
break;
}
}
}
// }
}
}
}
----------------------------------------------
Class and recursive method
public with sharing class testclass {
public account a = new account();
account s = new account();
public Account getnextparent( Account inboundparent,Map<ID,Account> inboundmap)
{
if (!inboundmap.isempty())
{
if (inboundparent.Is_VGE_Account__c)
{
inboundmap.clear();
return (inboundparent);
}
else
{
s = inboundmap.get(inboundparent.ParentID);
if ( s == null)
{
inboundmap.clear();
return (inboundparent);
}
inboundmap.remove(inboundparent.Id);
a = getnextparent(s,inboundmap);
return(a);
}
}
else
{
return a;
}
}
}
http://boards.developerforce.com/t5/Apex-Code-Development/A-Pattern-for-Obtaining-all-record-in-a-Hierarchy/td-p/487453
See if this helps you out:
http://www.forcetree.com/2011/04/tree-view-in-visualforce-page.html
Hit kudos adn mark this as answer if it helps you out.