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 3. Then 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
It is very helpfull to me....
ReplyDeleteThankYou
thanks bhargava... welcome
ReplyDeletehey although nice usage of static but...
ReplyDeleteNever go for updating the same record on after trigger..
better go for before trigger
@n3t_phir3 you are correct but there are many senario where you need this
ReplyDeleteex:
an insert trigger and you need the ID of current record then you have to use after insert.
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...
Deletebut 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..
ya you are right bro but there are scenario where you have to do update on after update trigger,
Deleteex:
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???
ok let me take another example for using this Run once logic,
ReplyDeletethere 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.. :)
yes, in that condition it wud surely help...!!! n
ReplyDeletewe can consume the power of static in tht situation