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 4 years, 6 months 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.

Popular Salesforce Blogs

Popular Salesforce Videos