Activity Forums Salesforce® Discussions How To Restrict Any Trigger To Fire Only Once?

  • shariq

    Member
    September 20, 2018 at 3:12 pm

    Hi,

    Triggers can fire twice, once before workflows and once after workflows.

    “The before and after triggers fire one more time only if something needs to be updated, If the fields have already been set to a value, the triggers are not fired again.”

    Workaround:

    public class HelperClass {

    public static boolean firstRun = true;

    }

    trigger affectedTrigger on Account (before delete, after delete, after undelete) {

    if(Trigger.isBefore){

    if(Trigger.isDelete){

    if(HelperClass.firstRun){

    Trigger.old[0].addError('Before Account Delete Error');

    HelperClass.firstRun=false;

    }
    }
    }
    }

    Thanks

  • Parul

    Member
    September 20, 2018 at 6:27 pm

    Triggers can fire twice, once before workflows and once after workflows, so must follow the best practice and order of execution of trigger.

     

    Thanks

  • shariq

    Member
    September 20, 2018 at 10:26 pm

    Hi Parul,

    There are some scenarios in which this case occurs so we use the static variable for it and there is one more way to achieve it by toggling custom setting records.

    Hope this helps.

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos