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
Naganjaneya Lakshman TadepalliNaganjaneya Lakshman Tadepalli 

How do you prevent a trigger running multiple times during a single save event?

Shashikant SharmaShashikant Sharma
You could achieve it by wrtiing your code like below:
 
Class code :

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


Trigger code :

trigger updateTrigger on anyObject(after update) {

    if(checkRecursive.runOnce())
    {
    //write your code here            
    }

}

You could read details here ; https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)
Abhi_TripathiAbhi_Tripathi
Shashikant's answer is also correct, their one more solution for it.

You can compare old value in trigger with the new value to stop the trigger to run again and again.

Like below
trigger AccountTrigger on Account( after insert) {
     for( Account acc: Trigger.newMap) {
     
            //Compare old value with new from the oldmap 
            if( acc.AccountNumber != oldMap.get(acc.Id).AccountNumber) {
                        
                   //DO SOMETHING
           }
     }
}
Juan Pablo Rodriguez M 5Juan Pablo Rodriguez M 5
It was so useful @Shashikant Sharma, thanks a lot! 👍🏻