Interview Questions   Tutorials   Discussions   Programs   

Apex - How to fire a trigger only once?

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

How to fire a trigger only once?


1 Answer

answered By Richa Upadhyay   0  

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.

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(){
     return true;
        return run;

Trigger code :

trigger updateTrigger on anyObject(after update) {

    //write your code here            

   add comment

Your answer

Join with account you already have



 Write A Tutorials
Online-Classroom Classes

  2 people following this question

  1 person following this tag

  Question tags

apex × 1

Asked 4 years and 7 months ago ago
Number of Views -395
Number of Answers -1
Last updated
1 year and 5 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!