SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Recursive Triggers Simplified

Before understanding Apex Recursive Triggers let’s understand more about the recursion itself.

Suppose you are driving a car and have been asked to travel on a circular road and exit when you see an exit sign. Suppose you took this literally and you keep on looping the circle waiting for that exit sign. This is a recursion that activity keeps on repeating and repeating without any sign of an exit.

Similarly happens in terms of the method call. When the method calls itself inside its definition/implementation it is said to be a recursive method call. In the programming world, it’s one of the few approaches to solving a problem. One good example is to find the factorial of a number.

Things go in a circle when there is no return condition or exit point and we say the method has gone into endless recursive calls.

Now coming back to Salesforce Triggers when does this happen?

It can happen when you are in one specific DML operation Trigger and do the same DML operation again inside that Trigger.

Example – You have written a Trigger for After Update on Contact and after doing some cool things you again update the Contact record in the same trigger. This will lead to endless recursive Update Trigger calls unless Salesforce Engine stops it forcefully. Refer to the code below to see a recursive trigger in action

trigger ContactTrigger on Contact (after update) {

    if(Trigger.isUpdate) {
        List<Contact> contactsToBeUpdated = new List<Contact>();

        // Some Cool Operation Here and contactsToBeUpdated was populated

        update contactsToBeUpdated;
        
    } 
}

So whenever a Contact record will be updated, it will execute the above code.

As in the above code, it enters the Trigger.isUpdate block it will update the contact record again which in turn will execute the Contact Update Trigger again and this whole operation will go into a loop or recursive Trigger calls as said.

So how to tame this behavior as it could be a valid technical scenario to update the contact record in certain conditions or processing in after update context.

To address these recursive calls Static variables come to the rescue. If you wish to know more about Static variables then feel free to take a quick peek here – Apex Static Variables
Please Come back here once done 🙂

Declare a static boolean variable in another class. Let’s say we called that class as RecursiveSwitch with a member variable isContactTriggerCalled. This static variable will be checked in Trigger to make a decision if you have to call the Trigger again. Refer to the below code to see Recursion handling in action:

public with sharing class RecursiveSwitch {
    public static Boolean isContactTriggerCalled = false;
}
trigger ContactTrigger on Contact (after update) {

    if(Trigger.isUpdate) {
        List<Contact> contactsToBeUpdated = new List<Contact>();

        // Some Cool Operation Here and contactsToBeUpdated was populated
        if(! RecursiveSwitch.isContactTriggerCalled) {
            update contactsToBeUpdated;
            RecursiveSwitch.isContactTriggerCalled = true;
        }
    } 
}

Now let’s break down the above code into smaller blocks to see the detailed action.

The below line creates a static variable in the class – RecursiveSwitch

public static Boolean isContactTriggerCalled = false;

The below block in ContactTrigger ensures that only update the contact record list whenever the isContactTriggerCalled variable is false. Once we do the contact records update then we will set the value as true in the isContactTriggerCalled variable. Thus, when the contact update trigger runs the next time it won’t be able to execute the update operation again.

if(! RecursiveSwitch.isContactTriggerCalled) {
    update contactsToBeUpdated;
    RecursiveSwitch.isContactTriggerCalled = true;
}

Now you are all set to drive and exit from the indefinite circular road of Apex Recursive Triggers 🙂

Feel free to visit the official docs for this and explore more.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


2 thoughts on “Apex Recursive Triggers Simplified

  • Anonymous

    This is not the most optimum solution to avoid Recursion, If there are more than 200 records updated in Bulk, The Trigger will run for first 200 records and will not run for the other 200. In a way it stops recursion but will disrupt the functionality.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *