Activity Forums Salesforce® Discussions Prevent trigger from multiple time executions in Salesforce?

  • Prevent trigger from multiple time executions in Salesforce?

    Posted by Anuj on May 11, 2020 at 11:39 AM

    A trigger is running multiple times during a single save event in an org. how can this be prevented ?

    MOHIT replied 6 years ago 2 Members · 1 Reply
  • 1 Reply
  • MOHIT

    Member
    May 11, 2020 at 2:36 PM

    To avoid these kind of situation we can use public class static variable.
    In StopRecursive class, we have a static variable which is set to true by default.
    public class StopRecursive {
    public static Boolean isFirstTime = true;
    }
    trigger SampleTrigger on Contact (after update){
    Set<String> accIdSet = new Set<String>();
    if(StopRecursive .isFirstTime){
    StopRecursive .isFirstTime = false;
    for(Contact conObj : Trigger.New){
    if(conObj.name != ‘SFDC’){
    accIdSet.add(conObj.accountId);
    }
    }
    // Use accIdSet in some way
    }
    }
    Once this trigger fired the static variable value will set to false and trigger will not fire again.
    Hope this helps!

Log In to reply.