I had a use case where i had to traverse through account hierarchy (whose business limit is infinite). So i tried coding myself(i am new to APEX :() and came up with a recursive kind of function.It works,but due to recursiveness ,it goes back to called function,and executes few lines below it.and i cannot find a exit kind of function in apex as well.
can someone please review my code and
see how can i go back to my trigger,immediately when i find the return value.
although code works fine,when i bulk test it sometimes fail,what is the maximum number of data we should pass via testclass for bulk test,it works for me when i create 200accounts but when i give accounts upto 1000 its fine.but after that it says too many code statements
My usecase is given detailed at : Account SOQL traverse
Code :
Trigger:
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;
}
}
}
// }
}
Called 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;
}
}
}