Why we use static variable in recursive trigger?

Why we use static variable in recursive trigger?

Hi, Recursion occurs when the same code is executed again and again. It can lead to an infinite loop and which can result in governor limit sometime.

What is the 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. Recursion is the process of executing the same task multiple times.

How do I stop a recursive process builder in Salesforce?

To avoid this the best workaround we have today is to go for apex customization and use static variables to avoid recursive loops. It would be great to have this feature implemented as it would avoid unnecessary apex customization !

What is a recursive trigger and how to avoid it?

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.

How are static variables used in a trigger?

Use static variables to store information that is shared within the confines of the class. All instances of the same class share a single copy of the static variables. For example, all triggers that are spawned by the same request can communicate with each other by viewing and updating static variables in a related class.

Why are static variables separated in a class?

Having static variables separated in a class will help you with the unit test by loading up the static variables with the required data and not rely on the fact that you have to trigger the trigger in order to populate with test data test your scenario.

How to avoid infinite loop in apex class?

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…