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
Newbie10Newbie10 

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;
            
        }
        
    }
}

 

 

 

Yoganand GadekarYoganand Gadekar

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.