All About Recursive Trigger in Salesforce | APPSeCONNECT

All About Recursive Trigger in Salesforce

All-About-Recursive-Trigger-in-Salesforce

Salesforce automates a business process with many tools like Workflow, Process Builder, Approval Process, Validation Rules, Triggers and so on. Every tool has its own advantages and limitations over the other. Workflow is used to update a field of an object on which workflow is created. Using the process builder, we can update the fields of other related objects. With triggers, we can update the fields of different objects.

If you want to run a piece of apex code at a particular time or at particular intervals of time we use Apex Scheduler. A Trigger is an Apex code which executes before or after inserting or modifying a record based on the condition provided. There are different types of triggers based on the action going to be performed. They are Before Triggers and After Triggers. Triggers allow modification of another record of the same type or different type.

Recursion is the process of executing the same task multiple times. The Recursive trigger is a trigger which calls itself repeatedly and leads to an infinite loop. There may be chances to hit the Governor Limit with Recursive Trigger. Below example shows what is a Recursive Trigger and how it can be avoided.

Consider the ‘Account’ object. It has a trigger which executes before inserting an Account, which is a ‘Before Insert’ trigger. When a new Account is to be inserted, the trigger will be fired and the trigger code will be executed.  Below is the trigger code:

trigger-code

The trigger calls a function called ‘Call’. Below is the code of the class which is called in the above trigger.

code-class

In the above ‘InsertAccount’ class we have a method called ‘call’. In this method, we are inserting a new ‘Account’.

Now I’ll try to insert a new Account in Salesforce.

account-in-Salesforce

When I tried to create a new record, I’m getting the error. The error is ‘maximum trigger depth exceeded’. In the above ‘Call’ method we are inserting a new Account. When we are trying to insert a new Account before insert trigger will be called. In this way, ‘before insert’ trigger will be called multiple times. It becomes a recursive trigger as there is no end for this process.Connect-Salesforce-ERP

This can be avoided using a ‘Static Boolean’ variable in Class.

A Static variable is a variable which is initialized when the class is loaded. All the static variables in a class are initialized before the object is created for the class. It is only static within the scope of ‘Apex Transaction’. A Single copy of ‘static variable’ is shared by all the instances of the same class. Static variables can’t be defined in inner classes.

Let’s see how to avoid ‘recursive trigger’ in Salesforce using a Static Boolean variable. In the above class, I’m defining a variable called ‘flag’ of type ‘Static Boolean’ with value as ‘true’ in the class ‘InsertAccount’.

Below is the updated code.

updated-code

The updated code works as below

When a new Account is inserted, ‘before insert’ trigger will be fired. This will call the ‘call’ method in ‘InsertAccount’ class. When this class is called ‘flag’ value is set as ‘true’.  When the ‘call’ method is called, the flag value is checked. If the flag value is ‘true’ then the ‘call’ method is executed. As this is the first execution, the flag value will be true and ‘call’ method will be executed. We are inserting another Account in the call method. When the ‘insert acc;’ in line 10 is executed, ‘before insert’ trigger will be executed. This will call the ‘call’ method in ‘InsertAccount’ class. When this class is called, the flag value is ‘false’, as it is updated in the previous transaction. ‘Call’ method will be called and the ‘flag’ value is checked. As it’s false, the code in the ‘if’ block will not be executed, which is from line 5 to line 10. So, the execution will be stopped here and the ‘Account’ will be inserted to Salesforce without any errors.

In this way, we can avoid recursive triggers in Salesforce using a Boolean Static Variable. This can also be avoided using a ‘Set’. As a set store only unique values, you can store the list of Accounts to be inserted in a ‘set’ and then insert them at once.

Now, you can easily connect your Salesforce CRM with the back-end ERP system to automate the business process!Connect-Salesforce-ERP

You may also like:
Salesforce Connect – Everything You Need To Know
APEX Scheduler in Salesforce: Step-by-Step Guide
Layouts in Salesforce – All You Need To Know

Team Veon