Get a Quote

Hey, do you want to share your amazing ideas with us, drop your requirements, we are keen to work with you on your fantastic project. We will try to reach you shortly, SEE YOU !

Tell us about you

Tell us about your Project

How To Avoid Recursive Triggers In Apex Code In Salesforce

Recursive Trigger In Apex

In this blog, we will learn how we can avoid the recursive triggers in apex code. In Salesforce it is recommended that there should be a single trigger for a single object. The reason behind this is you can not be sure about the sequence of occurrence for your triggers. Salesforce facilitates multiple things to automate your business process such as Workflow, Process Builder, and lastly APEX classes.

Also Read: How To Call Flows From Apex In Salesforce

What Is Recursion?

Consider a scenario where you have written an update code on a contact object whenever its parent account gets updated. To achieve this you are using the update trigger of account (Suppose trigger A) and updating your contacts. Suppose another developer is working on the same org and he wants to update the account object whenever a contact gets updated. Also, he is using a contact trigger(Suppose trigger B) to update the parent account. Now, what if admin update an Account object called John doe.Account object called John doe

As we can see this situation tends to call these triggers in a loop. This situation is called recursion. It can be due to workflow or process builder as well. So recursion occurs when the same piece of code runs again and again, in a non-ending loop or sometimes in an unwanted output.

hire salesforce developers

How to Avoid

In order to avoid this situation, we will go to a static variable. As we know static variables can be initialized only once. We will use this feature to avoid recursion in our code.

Let’s do this with an example.

public class RecursiveTriggerHandler{
     public static Boolean isExecuted = false;

trigger RecursiveTrigger on Account (before update){
    // Check your variable first. If it calls second time will set as true and your if the condition fails
        // Now since its first time set it to true 
        testTriggerHandler.isFirstTime = true;
        // Do your coding part here

No matter multiple people or developers working in your org or not it is always a best practice to keep your code secure from all known and possible issues. So I recommend using this. I hope this will help you. Keep Exploring.

Learn More: How To Fetch Records By Lightning Data Service In Salesforce

author profile
About Author
Author Name:
Virendra Sharma

Certified Magento 2 Consultant

Author Bio:

Virendra Sharma is an ecommerce tech-enthusiast and certified Magento Developer. With a decade of experience in Magento ecommerce development, he has been assisting ecommerce businesses across the globe to harness Magento capabilities by developing, maintaining, and improving clients’ eCommerce websites. A leader in his own rights his teammates see him as an avid researcher and a Magento evangelist.

About Emizentech:

An acclaimed IT solution provider and a leading Magento 2 development company with its expertise lie in delivering top of the line ecommerce solutions, Salesforce commerce cloud services, mobile app development, and many more verticals.

Develop Car Buy Sell App
06 Jul, 2020

Car Buy-Sell Mobile App Development - Cost & Features

Technology has changed the shopping habits and the needs of the customers. It has changed the way people have started […]
Read more
Mobile App Development
Convert Ecommerce Store to Mobile Commerce App
03 Jul, 2020

Why Convert Your Ecommerce Store To Mobile Commerce App

While running towards success, an eCommerce business everyone just dreams about living in the heart of the audience and ruling […]
Read more
Ecommerce Development
On Demand Laundry Mobile App Development Cost & Features
02 Jul, 2020

On-Demand Laundry Mobile App Development - Cost and Features

Home services, handyman services, food, and cab services are amongst the greatest beneficiaries of the evolving on-demand economy. Whether it […]
Read more
Mobile App Development

Let’s Talk Business

Reach out to us by following below given details, or simply drop us a text to start a conversation, we are here to provide best in class business solutions.