+ Start a Discussion
Manisha KumariManisha Kumari 

IsParentCheckboxTrig: System.LimitException: Too many SOQL queries: 101

i have written one trigger for account which is firing when  Account has some child account... the parent account has one checkbox which is becoming true when it has child account in its hierarchy. after deploying it to production i am getting this error.

IsParentCheckboxTrig: System.LimitException: Too many SOQL queries: 101

kidly help me to resolve this error.

Thanks!!

Below is the Trigger:

trigger IsParentCheckboxTrig on Account ( after insert,after update) {

if(!RecursionControl.stoprecursion4)
{

set<Id> AccIds= new set<Id>();
set<Id> AccIds1 = new set<Id>();
Map<Id,Id> accmap = new Map<Id,Id>();

if (trigger.Isinsert){
    for(Account acc: Trigger.new){
        If (acc.ParentId!= null ){
           AccIds.add(acc.ParentId);        
        } 
    }
    }
if (trigger.IsUpdate){
 
    for(Account acc1 : Trigger.old){
           if(acc1.ParentId != null)
           {
              accmap.put(acc1.Id, acc1.ParentId);
              System.debug('Trigger.old operation : '+acc1.ParentId);
           }
          }
    for(Account acc: Trigger.new){
        If (acc.ParentId!= null ){
          AccIds.add(acc.ParentId);
          }
        else if(acc.ParentId == null || acc.ParentId == '')
          {
          System.debug('___________Parent Id is null');
              if(accmap.containsKey(acc.Id))
              {
                 AccIds1.add(accmap.get(acc.Id));
                 System.debug('Trigger.new Operation '+acc.ParentId);
              }
          }           
 
    }
 
    }
 
    if(accIds != null && accIds.size() > 0){
        List<Account> accParentList = [ Select IsParent1__c , ID from Account where IsParent1__c != true and ID IN : accIDs] ;
    
        For( Account acc : accParentList ){
            acc.IsParent1__c = true ;
        }
     
     
     
        if( accParentList != null && accPArentList.size() > 0 ){
            RecursionControl.stoprecursion4 = true;
            update accParentList ;
        }
     
    }
 
    if(AccIds1 != null && AccIds1.size() > 0){
        List<Account> accParentList1 = [ Select IsParent1__c , ID from Account where ID IN : accIDs1];
        List<Account> accParentList2 = [ Select IsParent1__c , ID, parentId from Account where parentId  IN : accIDs1];
        Map<string,string> AccIDMap=new Map<string,string>();
        for(Account e:accParentList2 ){
            AccIDMap.put(e.parentId,e.id);
        }
     
        For( Account acc : accParentList1 ){
            system.debug('@@@@@@@@@@@@@@@@@'+acc+'-'+AccIDMap.containsKey(acc.id));
            if(!AccIDMap.containsKey(acc.id)){
                system.debug('@@@@@@@@@@@@@@@@@inside');
                acc.IsParent1__c = false;
            }
        }
     
        if( accParentList1 != null && accPArentList1.size() > 0 ){
            RecursionControl.stoprecursion4 = true;
            update accParentList1 ;
        }
    }
 
}
    }
Subhash GarhwalSubhash Garhwal
Hi Manisha,

try this one

trigger IsParentCheckboxTrig on Account (after insert,after update) {
   
    if(!RecursionControl.stoprecursion4) {

//Set to add parent iD
Set<Id> setParentAccIds = new Set<Id>();

//Loop through Account
        for(Account acc : Trigger.new) {

     //Check for condition
     if((Trigger.oldMap == null && acc.ParentId != null) || (Trigger.oldMap != null && acc.ParentId != Trigger.OldMap.get(acc.Id).ParentId)) {
    
  //Check for Parent Id
  if(acc.ParentId != null)
      setParentAccIds.add(acc.ParentId);
 
  //Check for Old Parent Id
  if(Trigger.oldMap != null && Trigger.oldMap.get(acc.Id).ParentId != null)
      setParentAccIds.add(Trigger.oldMap.get(acc.Id).ParentId);
     }
        }

//List of account
List<Account> accounts = new List<Account>();

//Loop through Account
for(Account acc : [Select Id, IsParent1__c, (Select Name From ChildAccounts) From Account Where Id IN : setParentAccIds]) {
    
     //Check for Child accounts
     if(acc.ChildAccounts.size() > 0)
         acc.IsParent1__c = true;
     else
         acc.IsParent1__c = false;
    
     //Add in list
            accounts.add(acc);
}
    }

    //Check for size
    if(accounts.size() > 0) {
        RecursionControl.stoprecursion4 = true;
        update accounts ;
    }
}
Manisha KumariManisha Kumari
getting one error Error: Compile Error: Variable does not exist: accounts at line 62 column 16
Subhash GarhwalSubhash Garhwal
Hi Manisha, 

Take  List<Account> accounts = new List<Account>(); at the start of the trigger

Like :

trigger IsParentCheckboxTrig on Account (after insert,after update) {
    
    //List of account
   List<Account> accounts = new List<Account>();

    if(!RecursionControl.stoprecursion4) {

Manisha KumariManisha Kumari
the code is not working..
Subhash GarhwalSubhash Garhwal
Hi Manisha,

It's working for me. Here the code that i have tried

trigger IsParentCheckboxTrig on Account (after insert,after update) {
   
    //Set to add parent iD
    Set<Id> setParentAccIds = new Set<Id>();
   
    //List of account
    List<Account> accounts = new List<Account>();
   
    //Loop through Account
    for(Account acc : Trigger.new) {

         //Check for condition
         if((Trigger.oldMap == null && acc.ParentId != null) || (Trigger.oldMap != null && acc.ParentId != Trigger.OldMap.get(acc.Id).ParentId)) {
   
              //Check for Parent Id
              if(acc.ParentId != null)
                  setParentAccIds.add(acc.ParentId);

              //Check for Old Parent Id
              if(Trigger.oldMap != null && Trigger.oldMap.get(acc.Id).ParentId != null)
                  setParentAccIds.add(Trigger.oldMap.get(acc.Id).ParentId);
         }
    }

   

    //Loop through Account
    for(Account acc : [Select Id, IsParent1__c, (Select Name From ChildAccounts) From Account Where Id IN : setParentAccIds]) {
   
        //Check for Child accounts
         if(acc.ChildAccounts.size() > 0)
             acc.IsParent1__c = true;
         else
             acc.IsParent1__c = false;
   
         //Add in list
         accounts.add(acc);
    }
   
    //Check for size
    if(accounts.size() > 0) {
        update accounts ;
    }
}

Manisha KumariManisha Kumari
Thank you so much.. its working fine..