Friday 22 November 2013

Run Once logic for avoiding multiple or recursive calling of trigger in Salesforce


             
Hi guys,
This is my first blog, so please correct my if I am wrong somewhere,
Here the simple but tricky question asked in many interviews of salesforce because it’s a common scenario comes many times that’s why I’m writing this,

Suppose you have to update a record in a trigger with after update event then what happens???

Here example:
trigger updateAccount on Account (after update) {
     List<Account> accList = new List<Account>();
     for (Account a : Trigger.new) {
        // some logic……
        //…………………………….
          accList.add(a);
     }
     update acclist;
}

If you execute the above code then, the trigger is recursively called and thoughts exception
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccount: maximum trigger depth exceeded 

Because you trying to do updation in trigger which is also updating same record so it called itself again and again and again upto its limit,

Here the solution of this problem,
1         1.  Make a public class.
2      2.    In that class take a static Boolean variable.
3      3Then create a static method, which has if statement which returns true first time only.

 Code:

-->> A class with static boolean variable and method:-


public class runOnce{
    public static Boolean firstRun = false;   
    public static Boolean run(){
        if(!firstRun){
            firstRun = true;
            return firstRun;
        }
        return false;
    }
}

-->> Now change in trigger,

trigger updateAccount on Account (after insert, after update) {   
     if(runOnce.run()){
         List<Account> accList = new List<Account>();
         for (Account a : [Select ID from Account where Id in :Trigger.new]) {
        // some logic……
        //…………………………….
              accList.add(a);
         }
         update acclist;
     }
}

Hope it helps, Happy Coding...:)

Anurag Jain


8 comments:

  1. It is very helpfull to me....

    ThankYou

    ReplyDelete
  2. hey although nice usage of static but...
    Never go for updating the same record on after trigger..
    better go for before trigger

    ReplyDelete
  3. @n3t_phir3 you are correct but there are many senario where you need this
    ex:
    an insert trigger and you need the ID of current record then you have to use after insert.

    ReplyDelete
    Replies
    1. okie.. as you said, there are some scenarios where we need ID of record and in that condition one should rather prefer after trigger say(after insert)..i totally agree wid you...
      but why u r doing any update or insert DML operation in "after trigger" which may produce recursive nature, rather go for before trigger
      and moreover use after trigger for post operations like sending email, callouts using that ID etc..
      If you have any scenario/example where we are bound to use after trigger which may cause recursion and we felt the need of static then share it but but don't do any update operation in that trigger..

      Delete
    2. ya you are right bro but there are scenario where you have to do update on after update trigger,
      ex:
      if you have need the value from formula field in Trigger and need to use this value in the same object for other purpose then what you do???

      Delete
  4. ok let me take another example for using this Run once logic,
    there are two object A and B if change some thing in object A it chages object B value and vice versa,
    then you have to write trigger on both objects to update other object values,
    then it creates cycle because if you update A it calls trigger to update B then B's trigger calls again A update,
    so in this situation this is the way to stop that Recursive calling of Trigger..

    hope its helps.. :)

    ReplyDelete
  5. yes, in that condition it wud surely help...!!! n
    we can consume the power of static in tht situation

    ReplyDelete