0

I have trigger which work like

 Account(before insert, before update, after insert, after update) 

I want that after update or after insert, no more before update or before insert fire. I mean trigger should get over and ends.

What would be the best approach for the same.

Thanks ......

2
  • If i understand correctly, you don't want to use the before insert and before update but only the after insert and after update. Then, that depends on your trigger coding. If you are changing any of the fields in same object, then you have to keep the update statements for the same object... So please specify some more information about your trigger... – Subhash Jun 17 '16 at 5:21
  • Thanks Subhash. I want all before insert, before update, after insert and after update. I fear how to handle recursion. Please advice – David Mycka Jun 17 '16 at 5:23
3

In order to avoid the situation of recursive call, make sure your trigger is getting executed only one time. To do so, you can create a class with a static boolean variable with default value true.

In the trigger, before executing your code keep a check that the variable is true or not.

Once you check make the variable false.

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 insert, after update) {

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

}
2
  • Thanks sfdcFanBoy. Could you please advice how to handle this from after udpate\insert. I mean Should i call checkRecursive in afterupdate\insert. Something like if(Trigger.isAfter){call function}. Thanks – David Mycka Jun 17 '16 at 5:24
  • Yes similarly, you need to add that condition check in all the events you require. After insert/after update – sfdcFanBoy Jun 17 '16 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.