Activity Forums Salesforce® Discussions A trigger is running multiple times during a single save event in an org.How can this be prevented?

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

    Posted by Avnish Yadav on September 12, 2018 at 9:01 am

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

    Parul replied 6 years, 2 months ago 3 Members · 2 Replies
  • 2 Replies
  • shariq

    Member
    September 12, 2018 at 12:12 pm

    Hi,

    Avoid recursive trigger in salesforce using static variable

    Recursion occurs when same code is executed again and again. It can lead to infinite loop and which can result to governor limit sometime. Sometime it can also result in unexpected output.

    It is very common to have recursion in trigger which can result to unexpected output or some error. So we should write code in such a way that it does not result to recursion. But sometime we are left with no choice.

    For example, we may come across a situation where in a trigger we update a field which in result invoke a workflow. Workflow contains one field update on same object. So trigger will be executed two times. It can lead us to unexpected output.

    Another example is our trigger fires on after update and it updates some related object and there is one more trigger on related object which updates child object. So it can result too infinite loop.

    To avoid these kind of situation we can use public class static variable.

    In RecursiveTriggerHandler class, we have a static variable which is set to true by default.

    public class RecursiveTriggerHandler{
    public static Boolean isFirstTime = true;
    }

    trigger SampleTrigger on Contact (after update){

    Set<String> accIdSet = new Set<String>();

    if(RecursiveTriggerHandler.isFirstTime){
    RecursiveTriggerHandler.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!

  • Parul

    Member
    September 12, 2018 at 4:08 pm

    Hi there,

    In RecursiveTriggerHandler class, we need to have a static variable which is need to set to true by default.

    public class RecursiveTriggerHandler{

    public static Boolean isFirstTime = true;
    }

    Find the sample trigger here:

    trigger AccountTrigger on Account(after update){

    Set<String> setOfAccId = new Set<String>();

    if(RecursiveTriggerHandler.isFirstTime){
    RecursiveTriggerHandler.isFirstTime = false;

    for(Account accObj : Trigger.New){
    if(accObj .name != ‘Test Name’){
    setOfAccId .add(accObj .accountId);
    }
    }

    // Use setOfAccId  in your logic
    }
    }

    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