How to stop Recursion Trigger Calls In Salesforce

by Rijwan Mohmmed
how-to-stop-recursion-trigger-calls-in-salesforce

Recursion occurs when the same code is executed again and again. Due to this, an error is populated maximum trigger depth is exceeded.

We can stop this by creating a static variable. With this variable trigger, code execute one time.

Apex Trigger:

trigger ContactTrigger on Conatct(before insert) {

    if(ContactTriggerHelper.runOnce){
        ContactTriggerHelper.runOnce = false;
        // trigger logic
    }   
 
}

Apex Helper Class:

public class ContactTriggerHelper { 
   public static boolean runOnce = true; 
}
What’s your Reaction?
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

You may also like

4 comments

Darci November 8, 2021 - 8:50 am

It’s fantastic that you are getting thoughts from this paragraph as well as from our dialogue
made at this place.

Reply
Deltax November 8, 2021 - 8:56 am

Thanks for feedback 😊

Reply
Praveen June 21, 2023 - 7:02 am

how can we stop the recursion in record triggered flow ??

Reply
Rijwan Mohmmed June 21, 2023 - 12:58 pm

Hi @Praveen,
There is a radio field on record trigger flow show you can select 2nd one and also put strong entry criteria.

Reply

Leave a Comment