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 MMichael M 

maximum trigger depth exceeded- how to fix

Hello, I sometimes get this error with my below trigger/class. Is there something I can change in my code that will allow me to avoid this error? Thank you so much. 

This is the error message:
RehospitalizationTrigger: maximum trigger depth exceeded Rehospitalization 
trigger event AfterInsert Rehospitalization trigger event AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event 
AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event 
AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event 
AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event 
AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event 
AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event 
AfterUpdate Rehospitalization trigger event AfterUpdate Rehospitalization trigger event AfterUpdate: [] Class.RehospLookupHospital.hospitalLookup: line 20, col    




This is the TRIGGER:
trigger RehospitalizationTrigger on Rehospitalization__c (after insert, after update) {
               for (rehospitalization__c r : trigger.new){
            if (r.discharge_location__c != null && r.Referral_Account__c == null){
                RehospLookupHospital.hospitalLookup(r.id, r.discharge_location__c);
            }
        }}

And here is the CLASS:
public class RehospLookupHospital {
    public static void hospitalLookup(id rehospId, string hospName){
        List<Rehospitalization__c> rehospToUpdate = new list<Rehospitalization__c>();
        cube_hospital__c ch;
        
         try{
         ch  = [select Id, Name, SF_Account_Id__c from cube_hospital__c where name = : hospName];
           }
        catch(exception e){
            system.debug(e.getMessage());
        }
        
        if (ch != null){
          id sfId = ch.SF_Account_Id__c;
            rehospitalization__c rehosp = [select id, referral_account__c from rehospitalization__c where id = :rehospId];
            rehosp.Referral_Account__c = sfId;
            update rehosp;
        }
        
    }
}
Best Answer chosen by Michael M
ShivankurShivankur (Salesforce Developers) 
Hi Michael,

Maximum Trigger Depth Exceeded Error Salesforce occurs mainly due to recursion in the trigger. Recursion can occur because of many reasons. Recursion occurs when the same code is executed again and again. It can lead to an infinite loop and which can result in governor limit sometime. Sometimes it can also result in unexpected output.

To avoid these kind of situation we can use public class static variable. We can solve this issue, you can set a condition on trigger so it will not be called recursively.

You can learn more here:
https://www.sfdcpoint.com/salesforce/avoid-recursive-trigger-salesforce/

Hope above information helps. Please mark as Best Answer so that it can help others in future.

​​​​​​​Thanks.

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Michael,

Maximum Trigger Depth Exceeded Error Salesforce occurs mainly due to recursion in the trigger. Recursion can occur because of many reasons. Recursion occurs when the same code is executed again and again. It can lead to an infinite loop and which can result in governor limit sometime. Sometimes it can also result in unexpected output.

To avoid these kind of situation we can use public class static variable. We can solve this issue, you can set a condition on trigger so it will not be called recursively.

You can learn more here:
https://www.sfdcpoint.com/salesforce/avoid-recursive-trigger-salesforce/

Hope above information helps. Please mark as Best Answer so that it can help others in future.

​​​​​​​Thanks.
This was selected as the best answer
Michael MMichael M
thank you!