Recursive Triggers:
In general Recursion is executing the same task repeatedly. The Recursive trigger occurs when same trigger called it self multiple times which results in infinite loop. Which would throw an error without commiting the changes to database.
Scenario:
Apex Trigger : Below trigger invokes when new account record created into the database . The RecursionExample trigger try to insert new record with the Account Name 'TestAccount' .
trigger RecursionExample on Account (After Insert) {
if(Trigger.isAfter&&Trigger.isInsert){
Account acc = new Account(Name = 'TestAccount');
Insert acc;
}
}
Assume if TestAccount inserted into the system ,This will again invoke same trigger 'RecursionExample ' ,Since it would be After insert operation .This will again create another account record with the Account Name 'TestAccount' and This will again invoke same trigger it will continue infinite times which leads to below shown error .
Solution : This Error can be avoided by setting the 'Boolean Static Variable' in the handler class.
Modified Trigger :
trigger RecursionExample on Account (After Insert) {
if(Trigger.isAfter&&Trigger.isInsert){
if(RecursionExampleHandler.isFirstTime){
RecursionExampleHandler.isFirstTime=false;
Account acc = new Account(Name = 'TestAccount');
Insert acc;
}
}
}
Handler :
public class RecursionExampleHandler {
public static Boolean isFirstTime=True;
}
When Trigger runs for first time ,Handler class will be invoked from the trigger and it will setup 'isFirstTime' Static variable to True .During First trigger Run 'isFirstTime' Static variable will be changed to 'False' along with the Account creation with account name 'TestAccount' .When trigger tries to Run for second time ,Trigger will not get run since 'isFirstTime' has been set to 'False' and stops the transaction right there.
We hope your clear with the process now .
If you still require further clarifications,Please let us know in the comments .
Happy Learning☺☺☺
In general Recursion is executing the same task repeatedly. The Recursive trigger occurs when same trigger called it self multiple times which results in infinite loop. Which would throw an error without commiting the changes to database.
Scenario:
Apex Trigger : Below trigger invokes when new account record created into the database . The RecursionExample trigger try to insert new record with the Account Name 'TestAccount' .
trigger RecursionExample on Account (After Insert) {
if(Trigger.isAfter&&Trigger.isInsert){
Account acc = new Account(Name = 'TestAccount');
Insert acc;
}
}
Assume if TestAccount inserted into the system ,This will again invoke same trigger 'RecursionExample ' ,Since it would be After insert operation .This will again create another account record with the Account Name 'TestAccount' and This will again invoke same trigger it will continue infinite times which leads to below shown error .
Solution : This Error can be avoided by setting the 'Boolean Static Variable' in the handler class.
Modified Trigger :
trigger RecursionExample on Account (After Insert) {
if(Trigger.isAfter&&Trigger.isInsert){
if(RecursionExampleHandler.isFirstTime){
RecursionExampleHandler.isFirstTime=false;
Account acc = new Account(Name = 'TestAccount');
Insert acc;
}
}
}
Handler :
public class RecursionExampleHandler {
public static Boolean isFirstTime=True;
}
When Trigger runs for first time ,Handler class will be invoked from the trigger and it will setup 'isFirstTime' Static variable to True .During First trigger Run 'isFirstTime' Static variable will be changed to 'False' along with the Account creation with account name 'TestAccount' .When trigger tries to Run for second time ,Trigger will not get run since 'isFirstTime' has been set to 'False' and stops the transaction right there.
We hope your clear with the process now .
If you still require further clarifications,Please let us know in the comments .
Happy Learning☺☺☺
I see how this would block recursive triggers, but won't it also block new triggers that happen before it is finished?
ReplyDeleteHi Cyyberspy,
ReplyDeleteFor entire transaction static variable remains false after it enters into the if block.so even if trigger event try to enter again condition will not get satisfied and ends the transaction.
Thanks,
ReplyDeleteSo the recursive calls are all in the same scope, so the static var remains the same. I get that.
How about if a new change happens while this process was running?
Would that be a new transaction, and so get a new copy of the static variable?
Yes you are right..
ReplyDeleteSo if it enters before trigger, the variable will be set to true and it won't execute after trigger?
ReplyDeleteThis approach won't work if the records were more than 200.
ReplyDelete