Apex Trigger – What is Recursion and Solutions to Avoid Recursion in Apex Trigger

Before moving on to Recursion in Apex, one needs to understand what is Apex Trigger.

Apex Trigger – Apex Trigger is a piece of code that executes Before and After a record is Inserted, Updated, Deleted in a Database.

Triggers are majorly used to Modify Same Records and Create or Updates Related Records, and configure Follow up Emails on Record Creation.

In order to take understanding as how “Apex Triggers” work and what other things we can do using “Apex Triggers” please refer to the following URL.

http://www.vinaychaturvedi.com/blog/apex-triggers-in-salesforce

Once we have understanding as how Apex Triggers work we need to avoid “Recursion” in it.

What is“Recursion” in Apex Trigger?

Recursion is when same code is executing again and again & it exceeds the “Governor Limit”.

When Trigger is called Over and Over and It Exceed the Maximum Trigger Depth that is Called Recursive Trigger.

If we Try to Insert Account After Insertion of Account Using Trigger Then it Call again & again and Exceed the Governor Limit (Maximum Trigger Depth) and gives Exception


trigger RecursionExample on Account (After Insert) {

if(Trigger.isAfter&&Trigger.isInsert){

Account acc = new Account(Name = 'Recursive Account');

Insert acc;

}

}

2nd

This is common mistake that most of the Developers do, let us move to a Solution for the same.

How to Avoid Recursion or How can we Write Recursion Free Apex Trigger?

Write Apex Handler Class with Static Boolean Variable and Method and return true if Boolean Variable is true otherwise False .

Check in Apex Trigger if Handler class Method Return True Then Execute your Logic


public Class HandlerClass{

public static booleanRecursionHandler = True;

public static booleanHandlerMethod(){

if(RecursionHandler){

RecursionHandler = False;

return true;

}

else{

return RecursionHandler;

}

}

}

Apex Trigger in Which Handler Class Called.


trigger RecursionExample on Account (After Insert) {

if(Trigger.isAfter&&Trigger.isInsert){

if(HandlerClass.HandlerMethod()){

Account acc = new Account(Name = 'Recursive Account');

Insert acc;

}

}

}

OR

If you do not want to use Handler Class Method thenWrite Apex Handler Class with Static Boolean Variable and Check Handler Class Variable in Apex Trigger IF it is True then Execute Your Logic and Make it false So that Trigger Can not execute Again.


public Class HandlerClass{

public static booleanRecursionHandler = True;

}

 


trigger RecursionExample on Account (After Insert) {

if(Trigger.isAfter&&Trigger.isInsert){

if(HandlerClass.RecursionHandler){

HandlerClass.RecursionHandler = False;

Account acc = new Account(Name = 'Recursive Account');

Insert acc;

}

}

}

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s