Interview Questions   Tutorials   Discussions   Programs   

Apex - How to fire a trigger only once?




592
views
asked kiran-57 March 6, 2012 04:11 AM  

How to fire a trigger only once?


           

1 Answer



 
answered By Richa Upadhyay   0  

Description
Many Developers face this issue because of recursive trigger, or recursive update trigger. For example in 'after update' trigger, Developer is performing update operation and this lead to recursive call.



Resolution
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 update) {

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

}
flag   
   add comment

Your answer

Join with account you already have

FF

Preview

 Write A Tutorials
Online-Classroom Classes
www.writeabc.com


  2 people following this question

  1 person following this tag

  Question tags

apex × 1

Asked 5 years and 9 months ago ago
Number of Views -592
Number of Answers -1
Last updated
2 years and 7 months ago ago

Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert