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
Lakshmi SLakshmi S 

Before Insert and Update Triggers - Restrict Recursive ?

Hi Team,

We have updated the field in same object using before insert & Update triggers, Is it necessary to use Recursive class for this trigger.

Please let me know.

Thanks,
Lakshmi.
Ravi Dutt SharmaRavi Dutt Sharma
Before insert and update triggers will not cause any recursion.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Lakshmi,

I trust you are doing very well.

It might possible that there's workflow on the object that can cause a field update. That will, in turn, trigger the update trigger.
A recursive trigger is one that is called over and over. It can lead to the infinite loop and which can result to governor limit sometime. Sometimes it can also result in unexpected output. If not controlled will result in this error: maximum trigger depth exceeded. So we should write code in such a way that it does not result to recursion.

To avoid recursion we can use a public class static variable. As per the order of execution, all Before Trigger and After Trigger will fire before the workflow update. To prevent Trigger to be fired the second time, after workflow field update we can set a static boolean variable in a Class.

Create a static variable in a class as true. Make it false before or after a trigger has been executed. Variable will become false when trigger runs for the first time (before workflow update). If the variable is true only then trigger will fire and if the trigger will try to execute the second time then it will not fire.

Handler Class:
public class TestHandler{
     public static Boolean isTriggerExecuted = true;
}

Trigger:
trigger testTrigger on Account (before update){
     
    if(TestHandler.isTriggerExecuted){
         
        //Logic

        TestHandler.isTriggerExecuted = false;
    }
}

I hope it helps you.

Kindly mark this as solved if the information was helpful.

Regards,
Khan Anas
Niraj Kr SinghNiraj Kr Singh

Hi Lakshmi,

According to the your question. Its not needed. Because While you are trying to update the a field it will invoke only Update event.
But make sure you have use the event handling cotext variable in your trigger. Like this ex:

Trigger <Trigger name> on <Your Object> (Before insert, Before Update) {
      //Your event based loggic here.
      if(Trigger.isBefore) {
              if(Trigger.isInsert){

               }
              if(Trigger.isUpdate) {

                   //YOUR UPDATE LOGIC WILL BE HERE. AND IT WILL GET EXECUTED ONLY IN "BEFORE UPDATE".
              }
       }
      
      if(Trigger.isAfter) {
          
      }
}



For further details you can go with below link:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

Might be it will help you.
Good luck !!

Thanks
Niraj

Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Lakshmi

You can call the class from trigger :
trigger testTrigger on Account (before update,before insert){
     
   if(trigger.before) 
    { 
     if(trigger.isInsert || trigger.isUpdate){
         
        //Logic
      
       
        }
    }
}

You can even check check the condition of trigger.old with trigger.new .And update the field only when there is difference in the values which you want to update.

To avoid recursive in triger you use booLean variable as: 
public class HandlerClass{
     public static Boolean isTriggerExecuted = true;
}
 
trigger testTrigger on Account (before update){
     
    if(HandlerClass.isTriggerExecuted){
         
       

        HandlerClass.isTriggerExecuted = false;
    }
}



Thanks
Bhargavi.
Lakshmi SLakshmi S
Hi Team,

Thanks for all,
I am not using static variable, based on workflow field update i am updating the field in before insert and update events using trigger. If i use static variable field is not updated using triggers.
trigger OppTrigger on Opportunity (before insert, before update, before delete, after insert, after update) {
    
    if(Trigger.isInsert){
        
        if(Trigger.isBefore){
     
             OppUpdateCls.beforeInsertMeth((List<Opportunity>)Trigger.New);
        }
                
    }else
        if(Trigger.isUpdate){
            if(Trigger.isBefore){
                
               OppUpdateCls.beforeUpdateMeth(Trigger.NewMap, Trigger.OldMap);
            }
                        
        }else
            if(Trigger.isDelete){
                if(Trigger.isBefore){
                    if(OpportunityDeleteCls.firstRun){
                        OpportunityDeleteCls.firstRun = false;
                        OpportunityDeleteCls.deleteOpp(Trigger.Old);
                    }
                }
            }
        
}

If i did any thing wrong, please let me know how can i resolve this issue.

Thanks
Lakshmi.