+ Start a Discussion
Sandeep M 1Sandeep M 1 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY on afterUpdate using trigger

I want to update record after insert and after update , i am writing trigger 

trigger opMetricsTrigger on Operational_Metrics__c (after insert,after update)
{
     Operational_Metrics__c[] op=null;
     Set<string> opIds = new Set<string>();
     Date d1,d2;
       List<Operational_Metrics__c> c= new LIST<Operational_Metrics__c>();
     private static boolean run = true;
     if(run)
     {
       run=false;
     for (Operational_Metrics__c e : Trigger.new)
     {
         if(e.u_id__c != null)
         opIds.add(e.u_id__c );
     }
      
     List<Operational_Metrics__c> opm = [select Unique_Pile_ID__c,u_id__C, Project__c,Composting_Method__c,Pile_Monitoring_Date__c,Average_Temperature__c,Moisture_Test__c,Pile_Monitoring_Action_Taken__c,Other_Action__c,Pile_Input_Register_Date__c,Total_Waste_Input_on_the_Pile__c,Pile_Completion_Stage__c,Date_Pile_COmpleted__c,Curing_Batch_Register_Date__c,No_of_Days__c  from operational_metrics__c where u_id__c in :opIds];
     for(Operational_Metrics__c ops : opm)
     {
        if(ops.Pile_Completion_Stage__c=='yes')
         {
             System.debug('records1 are '+ops.Date_Pile_COmpleted__c);
             d1=ops.Date_Pile_COmpleted__c;
         } 
         else if(ops.Curing_Batch_Register_Date__c!=null)
         {
             d2=ops.Curing_Batch_Register_Date__c;
             System.debug('records2 are '+ops.Curing_Batch_Register_Date__c);
    
         }
        if(d1!=null && d2!=null)
        {
           
            ops.No_of_Days_for_piling__c=d1.daysBetween(d2);
             System.debug('no of days '+ops.No_of_Days_for_piling__c);
            c.add(ops);
        }
       
     }
    

       update c;
    }
}

I am able to get all debug logs correctly but when i am updating the record it is throwing CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY error which causes recursive calls to the trigger. could any one help me out.
Anoop yadavAnoop yadav
Hi Sandeep,

You can not update current record in after event.
Use before trigger or you can use workflow field update.
Ramu_SFDCRamu_SFDC
See if this helps http://salesforce.stackexchange.com/questions/31388/cannot-insert-update-activate-entity-execution-of-afterinsert