Prevent Recursion in Apex Trigger: maximum trigger depth exceeded

6:26 PM

Recursion happens when code execution falls in a loop and keeps executing indefinitely. You get a run time exception 'maximum trigger depth exceeded'

To avoid such situations we use a static variable. Below is a simple example. In the code below the trigger fires on after update and updates the lead one more time. But, we want to run this for one time only. Setting the static boolean flag avoids executing the trigger the second time. Static variables live throughout the entire apex transaction and hence their value remains intact. Try removing the static keyword to get into the exception.