• Manisha Kumari
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
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 ;
        }
    }
 
}
    }
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 ;
        }
    }
 
}
    }
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 ;
        }
    }
 
}
    }