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
Michael MinnekeerMichael Minnekeer 

Maximum trigger depth

I am running into this issue, I have fixed it before by adding a boolean but even with it on this example I still get the same error. It's throwing the error on line 52. 
 
public class Account_HDL {

private static boolean updatecstimp = false;
private static boolean dontupdatecstimp = false;
private static boolean newreview = false;
    
    
public static void UpdateCSTImp(Account[] acc){
list<CST_Implementation__c> cstimplist = new List<CST_Implementation__c>();
Set<String> cstimpIds = new Set<String>();
list<Account> updateacclist = new List<Account>();
Set<String> updateaccIds = new Set<String>();


    
    for(Account accs : acc){
        //updateaccIds.add(accs.Id);
        updateacclist  = [select Id, CST_Fully_Implemented_On__c, CST_Implementation_Stage__c, Days_from_RC_to_Imp__c, Last_Review_Date__c, 
                 Number_of_CST_Implemented_Services__c, Number_of_CST_Imp_Services_Complete__c, Number_of_CST_Imp_Services_Not_Complete__c, 
                 Latest_CST_Implementation__c, Regarding_Annual_Review__c, Review_FP_Result__c, Review_ROA_Result__c, Review_Super_Result__c 
                 from Account where Id =: trigger.new];
        
            Account oldAcc = (Account)Trigger.oldMap.get(accs.Id);
        
        for(Account updateacc : updateacclist){
           if(oldAcc.Regarding_Annual_Review__c != accs.Regarding_Annual_Review__c && accs.Regarding_Annual_Review__c != null
              && dontupdatecstimp == false && newreview == false){
               dontupdatecstimp = true;
               updateacc.CST_Fully_Implemented_On__c  = null;
               updateacc.CST_Implementation_Stage__c  = ''; 
               updateacc.Last_Review_Date__c  = null; 
               updateacc.Number_of_CST_Implemented_Services__c  = null; 
               updateacc.Number_of_CST_Imp_Services_Complete__c  = null; 
               updateacc.Number_of_CST_Imp_Services_Not_Complete__c  = null; 
               updateacc.Review_FP_Result__c  = ''; 
               updateacc.Review_ROA_Result__c  = ''; 
               updateacc.Review_Super_Result__c  = ''; 
               //create new cst imp for annual review   
                            List<CST_Implementation__c> addcstimplist = new list<CST_Implementation__c>();
                           for(CST_Implementation__c newcstimp : addcstimplist){
                            
                            addcstimplist.add(new CST_Implementation__c(
                            Name = 'Annual Review',
                            Related_Account__c = updateacc.Id
                            ));
                           }
                           insert addcstimplist;
                        
            }
           }
             if(updateacclist.size()>0 && dontupdatecstimp == true && newreview == false){
                update updateacclist;
                newreview = true;
               } 
        cstimpIds.add(accs.Latest_CST_Implementation__c);
        cstimplist = [select Id  from CST_Implementation__c where ID in : cstimpIds];
        for(CST_Implementation__c relatedcstimp : cstimplist){
            if(dontupdatecstimp == false){
            if( oldAcc.CST_Fully_Implemented_On__c != accs.CST_Fully_Implemented_On__c){
                relatedcstimp.CST_Fully_Implemented_On__c = accs.CST_Fully_Implemented_On__c;
                updatecstimp = true;
            }
            if( oldAcc.CST_Implementation_Stage__c != accs.CST_Implementation_Stage__c){
                relatedcstimp.CST_Implementation_Stage__c = accs.CST_Implementation_Stage__c;
                updatecstimp = true;
            }
            if( oldAcc.Last_Review_Date__c != accs.Last_Review_Date__c){
                relatedcstimp.Last_Review_Date__c = accs.Last_Review_Date__c;
                updatecstimp = true;
            }
            if( oldAcc.Number_of_CST_Implemented_Services__c != accs.Number_of_CST_Implemented_Services__c){
                relatedcstimp.Number_of_CST_Implemented_Services__c = accs.Number_of_CST_Implemented_Services__c;
                updatecstimp = true;
            }
            if( oldAcc.Number_of_CST_Imp_Services_Complete__c != accs.Number_of_CST_Imp_Services_Complete__c){
                relatedcstimp.Number_of_CST_Imp_Services_Complete__c = accs.Number_of_CST_Imp_Services_Complete__c;
                updatecstimp = true;
            }
            if( oldAcc.Number_of_CST_Imp_Services_Not_Complete__c != accs.Number_of_CST_Imp_Services_Not_Complete__c){
                relatedcstimp.Number_of_CST_Imp_Services_Not_Complete__c = accs.Number_of_CST_Imp_Services_Not_Complete__c;
                updatecstimp = true;
            }
            if( oldAcc.Review_FP_Result__c != accs.Review_FP_Result__c){
                relatedcstimp.Review_FP_Result__c = accs.Review_FP_Result__c;
                updatecstimp = true;
            }
            if( oldAcc.Review_ROA_Result__c != accs.Review_ROA_Result__c){
                relatedcstimp.Review_ROA_Result__c = accs.Review_ROA_Result__c;
                updatecstimp = true;
            }
            if( oldAcc.Review_Super_Result__c != accs.Review_Super_Result__c){
                relatedcstimp.Review_Super_Result__c = accs.Review_Super_Result__c;
                updatecstimp = true;
            }
            }
        } 
        if(cstimplist.size()>0 && updatecstimp == true && dontupdatecstimp == false){
        update cstimplist;
        }
    }
       
}
}

 
Best Answer chosen by Michael Minnekeer
SaranSaran
Hi Michael,

Is there any reason why you are doing this in after update.

Because you are updating all the accounts in trigger.new.

So you can so the same thing in before update and remove the statement at line no 36(update UpdateaccList);
 
public class Account_HDL {

	public static void UpdateCSTImp(Account[] acc){


		for(Account accs : updateacclist  ){
			Account oldAcc = (Account)Trigger.oldMap.get(accs.Id);
			if(oldAcc.Regarding_Annual_Review__c != accs.Regarding_Annual_Review__c && accs.Regarding_Annual_Review__c != null)
			{               
				accs.CST_Fully_Implemented_On__c  = null;
				accs.CST_Implementation_Stage__c  = ''; 
				accs.Last_Review_Date__c  = null; 
				accs.Number_of_CST_Implemented_Services__c  = null; 
				accs.Number_of_CST_Imp_Services_Complete__c  = null; 
				accs.Number_of_CST_Imp_Services_Not_Complete__c  = null; 
				accs.Review_FP_Result__c  = ''; 
				accs.Review_ROA_Result__c  = ''; 
				accs.Review_Super_Result__c  = ''; 
			}
		}    
	}
}

Also you can chage your trigger as,
 
trigger AccountTrigger on Account (before update, after update) {
   if(Trigger.isBefore){ 
        Account_HDL.UpdateCSTImp(Trigger.new);
    }
}

Thanks!

All Answers

Vishal_GuptaVishal_Gupta
Hi Michael,

you are updating account records in triiger of Account and in this way it is recusrsively calling, you can update the accounts values in before Insert/Update, so in this way you dont need to make DML on Account again.

Please let me know if I can help you in it. my contact details are on my profile.

Thanks,
Vishal
 
Michael MinnekeerMichael Minnekeer

Hi Vishal

Here is the trigger calling it but I get the same error if I try either:
 

trigger AccountTrigger on Account (before update, after update) {
   if(Trigger.isUpdate && Trigger.isBefore){ 
        Account_HDL.UpdateCSTImp(Trigger.new);
    }
}
or 
trigger AccountTrigger on Account (before update, after update) {
   if(Trigger.isUpdate && Trigger.isafter){ 
        Account_HDL.UpdateCSTImp(Trigger.new);
    }
}

 
Vishal_GuptaVishal_Gupta
Hi Michael,

There are need to do many changes in your helper class of trigger like SOQL written in For loop, DMlL in for loop etc.

Is it possible for you to share your org with me for 1-2 hour, I will do the changes. 

Thanks,
Vishal
SaranSaran
Hi Michael,

As Vishal said there is many changes to be done in your code. You are trying to loop same set of data inside another loop. And in before update you cannot update the current obj record.

Else let us know what you are exactly trying to do. So that it will be a hint fot us to start.

Thanks!
Michael MinnekeerMichael Minnekeer
Ok I have removed the other stuff so it is clear what I need help with. I just need to fix the recirsive error on updating the record in trigger.new
public class Account_HDL {

private static boolean updatecstimp = false;
private static boolean dontupdatecstimp = false;
private static boolean newreview = false;
    
    
public static void UpdateCSTImp(Account[] acc){
list<CST_Implementation__c> cstimplist = new List<CST_Implementation__c>();
Set<String> cstimpIds = new Set<String>();
list<Account> updateacclist = new List<Account>();
Set<String> updateaccIds = new Set<String>();
updateacclist  = [select Id, CST_Fully_Implemented_On__c, CST_Implementation_Stage__c, Days_from_RC_to_Imp__c, Last_Review_Date__c, 
                 Number_of_CST_Implemented_Services__c, Number_of_CST_Imp_Services_Complete__c, Number_of_CST_Imp_Services_Not_Complete__c, 
                 Latest_CST_Implementation__c, Regarding_Annual_Review__c, Review_FP_Result__c, Review_ROA_Result__c, Review_Super_Result__c 
                 from Account where Id =: trigger.new];

    
    for(Account accs : updateacclist  ){
        Account oldAcc = (Account)Trigger.oldMap.get(accs.Id);
           if(oldAcc.Regarding_Annual_Review__c != accs.Regarding_Annual_Review__c && accs.Regarding_Annual_Review__c != null
              && newreview == false){
               //dontupdatecstimp = true;
               accs.CST_Fully_Implemented_On__c  = null;
               accs.CST_Implementation_Stage__c  = ''; 
               accs.Last_Review_Date__c  = null; 
               accs.Number_of_CST_Implemented_Services__c  = null; 
               accs.Number_of_CST_Imp_Services_Complete__c  = null; 
               accs.Number_of_CST_Imp_Services_Not_Complete__c  = null; 
               accs.Review_FP_Result__c  = ''; 
               accs.Review_ROA_Result__c  = ''; 
               accs.Review_Super_Result__c  = ''; 
            }
      }
             if(updateacclist.size()>0 && newreview == false){
                update updateacclist;
                newreview = true;
               } 
       
       
}
}

 
SaranSaran
Hi Michael,

Is there any reason why you are doing this in after update.

Because you are updating all the accounts in trigger.new.

So you can so the same thing in before update and remove the statement at line no 36(update UpdateaccList);
 
public class Account_HDL {

	public static void UpdateCSTImp(Account[] acc){


		for(Account accs : updateacclist  ){
			Account oldAcc = (Account)Trigger.oldMap.get(accs.Id);
			if(oldAcc.Regarding_Annual_Review__c != accs.Regarding_Annual_Review__c && accs.Regarding_Annual_Review__c != null)
			{               
				accs.CST_Fully_Implemented_On__c  = null;
				accs.CST_Implementation_Stage__c  = ''; 
				accs.Last_Review_Date__c  = null; 
				accs.Number_of_CST_Implemented_Services__c  = null; 
				accs.Number_of_CST_Imp_Services_Complete__c  = null; 
				accs.Number_of_CST_Imp_Services_Not_Complete__c  = null; 
				accs.Review_FP_Result__c  = ''; 
				accs.Review_ROA_Result__c  = ''; 
				accs.Review_Super_Result__c  = ''; 
			}
		}    
	}
}

Also you can chage your trigger as,
 
trigger AccountTrigger on Account (before update, after update) {
   if(Trigger.isBefore){ 
        Account_HDL.UpdateCSTImp(Trigger.new);
    }
}

Thanks!
This was selected as the best answer
Michael MinnekeerMichael Minnekeer
Yep that worked thanks, It was pretty obvious I shouldnt of needed the DML when breaking it down a bit.