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
GMASJGMASJ 

Too Many SOQL when try to call method

Hi, 

  I am trying to use some logic to get all parent and all child accounts from below code Please suggest me how to change.
 
public static voidAllAccounts (Id ChildId){
     
    Set<id> allacts = ReceiveAllAccounts(ChildId); // I am trying to get all account getting too many soql 
    
    For(Account acc : [select id,name, parentid, (Select id,NSE_1__c,NSE_2__c from contacts)from account where id = :allacts]){
        
        For(Contact c : acc.contacts ){
            
            if(c.NSE_1__c == true)               
                 NSEint1 = NSEint1 +1;
                 //NSEmap1.put(acc.id,NSEint1);
                 
            
            if(c.NSE_2__c == true)
                NSEint2 = NSEint2 + 1;                          
                //NSEmap2.put(acc.id,NSEint2);              
            
        }
         NSEmap1.put(acc.id,NSEint1);
         NSEmap2.put(acc.id,NSEint2);
         
        if(acc.parentid != null)            
           childToParnet(acc.parentid);
        
     }
  }
  
  
  
public static set<id> ReceiveAllAccounts(Id PAccountid){
 
  set<id> setactid = new set<id>();
  id accountId = PAccountid;
  
  // Get All Parents 
  Account[] allparents = new Account[] {};
            
  Set<Id> parentIds = new Set<Id>{accountId};
                
  Account[] parent;
        
  do {
            
   parent = [select Id,ParentId, Name from Account where Id in :parentIds];
            
   allparents.addAll(parent);
            
   parentIds.clear();
            
   for (Account par : parent) 
                
     parentIds.add(par.ParentId);
            
    } while (parent.size() > 0);
        
  list<Account> Act = [select id, name from account where id in :allparents];
        
  for(Account A : Act){    
     system.debug('Parent Accounts ' + a.name);  
     setactid.add(a.id);
     }  
  
  
   // Get all child
    Account[] allChildren = new Account[] {};

    Set<Id> childrenIds = new Set<Id>{accountId};

    Account[] children;

    do {

    children = [select Id, Name from Account where ParentId in :childrenIds];

    allChildren.addAll(children);

    childrenIds.clear();

    for (Account child : children) 

      childrenIds.add(child.Id);

    } while (children.size() > 0);

    list<Account> Acts = [select id, name from account where id in :allChildren];

     for(Account A : Acts){
 
     system.debug('Child Accounts ' + a.name);       
      setactid.add(a.id);
     }
  
      return setactid;


}

Thanks
Sudhir
ManojjenaManojjena
HI Sudhir ,

Check line number 83

     while (children.size() > 0)
     list<Account> Acts = [select id, name from account where id in :allChildren];



and try to change with below 

if(children.size() > 0){
   list<Account> Acts = [select id, name from account where id in :allChildren];
}
Let me knwo if it helps !!!
Thanks 
Manoj
 
GMASJGMASJ
Hi Manoj,

   It is DO WHILE loop I am not sure replacing with IF condition will resove please see the code. 

Thanks
Sudhir
ManojjenaManojjena
HI Sudhir ,

Put a debug log in your while loop and check how many times your loop is executing ,Basically if we should noty write query inside loop .

Please check once and let me know .

Thanks 
Manoj