0

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.

1
  • check the reason for CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY. See the full exception message. May be some validation failing, required fields missing, etc.
    – highfive
    Commented Jul 1, 2014 at 10:38

1 Answer 1

1

I solved my issue using a static Boolean variable:

public class TriggerContext{
    private static boolean firstRun = true;

    public static boolean isFirstRun() {
        return firstRun;
    }

    public static void setFirstRunFalse(){
        firstRun = false;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .