Slaesforce FAQ

how to prevent recursive trigger in salesforce

by Prof. Arielle Koelpin III Published 2 years ago Updated 2 years ago
image

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.

How to avoid recursion when using a trigger?

You have to write in the future method. To avoid recursion in this particular scenario, you just need to check the record to see if the trigger is an insert or if the recordtypeid has changed.

What is recursive trigger in Laravel?

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. To avoid recursive triggers you can create a class with a static Boolean variable with default value true.

What is recursive trigger in Munna?

Munna. A recursive trigger is one that will end up calling itself again. For example, if you have a trigger on accounts that updates contacts, and a trigger on contacts that updates accounts, it is possible that the account update could update contacts that would in turn update the account... this will cause errors.

image

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.

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=‘[email protected]’; Update con; }} Static in Salesforce are per transaction, so the value will be true only for the current transaction.

What is a recursive trigger?

A recursive trigger is one that will end up calling itself again. For example, if you have a trigger on accounts that updates contacts, and a trigger on contacts that updates accounts, it is possible that the account update could update contacts that would in turn update the account... this will cause errors.

Can a record cause another trigger to fire?

However, that record may then cause another trigger to fire, which in turn causes another to fire, and so on. You don't know how to stop that recursion. Using a static variable in an Apex class to avoid an infinite loop.

image
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9