+ Start a Discussion
GMASJGMASJ 

Trigger Recursion

Hi, 

  I want to update set of records on same object when condition is met below it the logic implemented but its not working 
 
trigger CaseTrigger on Case (After Insert, After Update){
   
   if(Trigger.isAfter && Trigger.isUpdate){
     CaseTriggerUtils.processUpdate(Trigger.newMap, Trigger.oldMap);     
    } 
}


public class CaseTriggerUtils {

 public static void CaseUtils(List<Case> newLst) {  

   public static void processUpdate(Map<id, Case> newMap, Map<id, Case> oldMap) {
      String CaseSubject;
      list<Case> caseLst=new list<Case>();
      boolean isExecuted = True;
      
      for (Case c : newMap.values()) {
           CaseSubject = c.subject;
       }
      
      list<case> ctendays = [select id, casenumber,subject,createddate,ownerid,owner.name from case where CreatedDate = this_year and subject like :('%' + CaseSubject + '%') order by createddate asc limit 10];
      for(case ctndays : ctendays){
         system.debug('Existing Cases :'  + ctndays.casenumber + ' ' + ctndays.subject + ' ' + ctndays.createddate + ' ' + ctndays.owner.name);      
         system.debug('Latest Case Owner ' + ctendays[0].owner.name); 
       
        ctndays.ownerid = ctendays[0].ownerid;
        ctndays.Duplicate_With__c = ctendays[0].casenumber;
        caseLst.add(ctndays);
      }  
      
      if(isExecuted) //To check whether the trigger is not get stuck in recursion if we are updating the same object
        {
          isExecuted=False;
          update caseLst;
        }  
    }
 }
}

I am getting below error 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CaseTrigger caused an unexpected exception, contact your administrator: CaseTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 5002D000004JCQhQAO; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CaseTrigger: maximum trigger depth exceeded Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate Case trigger event AfterUpdate: []: Class.CaseTriggerUtils.processUpdate: line 68, column 1

Please suggest me how to fix this issue. 

Thanks
GMASJ
Raj VakatiRaj Vakati
Create an apex class like below .. In My opinion, you no need after insert and after update trigger and you can able to use the before insert and before update trigger 

Refer to this link 

https://help.salesforce.com/articleView?amp;language=en_US&id=000199485&type=1


 
public Class AvoidRecursion{
    private static boolean firstRun = true;
    public static boolean isFirstRun(){
    if(firstRun){
      firstRun = false;
      return true;
    }else{
        return firstRun;
    }
    }
}


Update the trigger like below
 
trigger CaseTrigger on Case (After Insert, After Update){
   
   if(Trigger.isAfter && Trigger.isUpdate){
    if(AvoidRecursion.isFirstRun())
    {
     CaseTriggerUtils.processUpdate(Trigger.newMap, Trigger.oldMap);   
}	 
    } 
}

 
GMASJGMASJ
Hi Raj, 

   I tried with Before Insert and Before Update it was not working. getting below error. 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CaseTrigger caused an unexpected exception, contact your administrator: CaseTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 1 with id 5002D000004JCQmQAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 5002D000004JCQm) is currently in trigger CaseTrigger, therefore it cannot recursively update itself: []: Class.CaseTriggerUtils.processUpdate: line 66, column 1


Thanks
Sudihr