How to handle recursive triggers in salesforce

image

How do you overcome a recursive trigger in Salesforce? To avoid the situation of recursive call, we have to write code in such a way that the trigger will execute one time. To do so, we can create a class with a static Boolean variable with default value true.

To avoid recursive triggers you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.Feb 23, 2020

Full
Answer

How to avoid recursive triggers in Salesforce apex?

Using static boolean variable in an apex class (we should not keep static boolean variable inside of the trigger) we can avoid recursive triggers. When you want to write a trigger that creates a new record as part of its processing logic. However, that record may then cause another trigger to fire, which in turn causes another to fire, and so on.

What is a recursive trigger?

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.

What is a trigger in Salesforce?

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.

How to avoid an infinite loop in Salesforce apex?

Using a static variable in an Apex class to avoid an infinite loop. A static variable is local to the context of a web request (or test method during a call to runTest ()), so all triggers that fire as a result of a user’s action which has access to it. Let’s understand some scenarios…

image


How do you handle trigger recursion?

Handle recursion – To avoid the recursion on a trigger, make sure your trigger is getting executed only one time. You may encounter the error : ‘Maximum trigger depth exceeded’, if recursion is not handled well.


What is recursive trigger in Salesforce with example?

A recursive trigger is one that performs an action, such as an update or insert, which invokes itself owing to, say something like an update it performs. eg in a before trigger, if you select some records and update them, the trigger will invoke itself. “Static variables are only static within the scope of the request.


What are the best practices for triggers in Salesforce?

Best Practice to Follow while writing trigger One Trigger Per Object. … Logic-less Triggers. … Context-Specific Handler Methods. … Bulkify your Code. … Avoid using DML statements and SOQL Queries inside FOR Loops. … Using Collections, Streamlining Queries, and Efficient For Loops. … Querying Large Data Sets.More items…•


Can we call trigger from another trigger in Salesforce?

If trigger starts working from another trigger, they are executed in same transaction. So as you can see, they both share limits in one transaction.


How can you avoid maximum trigger depth exceeded?

To avoid these kind of situation we can use public class static variable. We can solve this issue, you can set a condition on trigger so it will not be called recursively.


How do you Bulkify triggers?

How to Bulkify trigger CodeTrigger Best Practices | Sample Trigger Example | Implementing Trigger Framework.1) One Trigger Per Object. … 2) Logic-less Triggers. … 3) Context-Specific Handler Methods. … 4) Bulkify your Code. … 5) Avoid SOQL Queries or DML statements inside FOR Loops.More items…•


Can we write two triggers on same object?

Multiple Triggers on the same object Writing multiple triggers renders the system unable to recognize the order of execution. Moreover, each trigger that is invoked does not get its own governor limits. Instead, all code that is processed, including the additional triggers, share those available resources.


How many records trigger can handle?

Triggers execute on batches of 200 records at a time. So if 400 records cause a trigger to fire, the trigger fires twice, once for each 200 records.


Can we write multiple trigger on single object?

If your packages/applications have domain logic on the same object, it is totally fine to implement multiple triggers per package.


Can one trigger initiate another trigger?

Both DML and DDL triggers are nested when a trigger performs an action that initiates another trigger. These actions can initiate other triggers, and so on. DML and DDL triggers can be nested up to 32 levels. You can control whether AFTER triggers can be nested through the nested triggers server configuration option.


Can we send email through trigger?

You need to create an apex trigger on contact object which send email when contact will inserted. List sendTo = new List(); sendTo. add(myContact.


Can we call batch Apex from trigger?

2. Batch Apex can be invoked using an Apex trigger. But the trigger should not add more batch jobs than the limit.


What is a recursive trigger?

A recursive trigger is one that performs an action, such as an update or insert, which invokes itself owing to, say something like an update it performs. eg in a before trigger, if you select some records and update them, the trigger will invoke itself. To avoid, static variable ‘locks’ are used. Illustrated in the salesforce doc.


Why use static variables in Salesforce?

“Static variables are only static within the scope of the request. They are not static across the server, or across the entire organization. Use static variables to store information that is shared within the confines of the class.


What is recursion in a game?

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.


What is trigger in Apex?

A Trigger is an Apex code which executes before or after inserting or modifying a record based on the condition provided.


What are the different types of triggers?

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.


How to avoid Recursive Trigger in Salesforce?

To avoid recursive triggers you can create a class with a static Boolean variable with default value true. In the trigger, before executing your code keep a check that the variable is true or not. Once you check make the variable false.


Avoiding Recursive Triggers

The Apex Developers Guide surprisingly does not say much about this, aside from a reference in the Static and Instance topic.


Best practice for triggers

One trigger per object so you don’t have to think about the execution order as there is no control over which trigger would be executed first.


Best practice for triggers

One trigger per object so you don’t have to think about the execution order as there is no control over which trigger would be executed first.


Apex Trigger

Trigger feedback on contact (after update) { if (!checkRecursive.firstcall) { checkRecursive.firstcall = true; Id conId; for (contact c:trigger.new) { conId=c.Id; } Contact con= [select id, name from contact where id!=:conId limit 1]; con.email=‘test@gmail.com’; Update con; }} Static in Salesforce are per transaction, so the value will be true only for the current transaction.

image

Leave a Comment