Demo Trigger.isExecuting Context Variable

When building robust and scalable Apex code in Salesforce, one of the most important things you’ll encounter is understanding the context in which your code is being executed. Whether it’s coming from a trigger, a Lightning controller, or a batch process, knowing where your logic is being invoked allows you to tailor behavior accordingly and avoid unnecessary or duplicate operations.

This blog post focuses on the powerful yet often overlooked Trigger context variable Trigger.isExecuting, which allows developers to differentiate between trigger-based execution and non-trigger-based execution (like calling a method from a controller or utility class).

In this real-world example, we demonstrate how to implement Trigger.isExecuting in your Apex handler class to write smarter, context-aware logic that behaves differently depending on how it is invoked. Whether you’re a beginner still wrapping your head around trigger contexts or an experienced developer looking to enforce clean coding practices, this tutorial is packed with insights you’ll find valuable.

⚙️ What Is Trigger.isExecuting?


Trigger.isExecuting is a built-in Boolean variable available only within Apex classes or triggers when code is executed in the context of a DML-triggered event (like insert, update, delete, or undelete). It returns:

  • true if the current code is being executed due to a trigger firing

  • false if the code is being executed from outside the trigger context (e.g., from a controller, batch job, scheduled class, or anonymous Apex)

This is particularly useful when you want to prevent double execution, isolate trigger-specific logic, or even enable dual-purpose classes that work across both UI and backend environments.

🔍 What You’ll Learn in This Blog


  • What Trigger.isExecuting is and why it matters

  • How to build classes that adapt based on where they’re called from

  • How to avoid repeating logic in both triggers and controller classes

  • Why using Trigger.isExecuting can help prevent unexpected behavior

  • Best practices for making your Apex classes reusable and context-safe

  • How to debug and log trigger execution status effectively

🧪 Real-World Scenario


In this example, we have a handler method handleAccount() in a class called AccountHandler. It accepts a list of Account records and includes a debug log for Trigger.isExecuting.

Inside the method:

  • If Trigger.isExecuting is true, it means the method is being invoked by an Apex trigger, so the system can perform trigger-specific actions like logging, field updates, or invoking sharing logic.

  • If false, it means the method is being invoked externally—like from a Visualforce controller, a LWC backend call, or even a test method—so a different logic path can be followed.

This is an excellent example of flexible design, enabling your code to work under multiple scenarios without duplication.

🎯 Why This Pattern Matters


Imagine you’re using the same utility method to update or process Accounts both:

  1. From a before insert trigger, and

  2. From a custom button on a record page

Without context-aware checks like Trigger.isExecuting, your logic could run redundantly or inappropriately, leading to performance issues or unexpected side effects. This pattern empowers you to keep separation of concerns intact and optimize user experience and backend reliability.

👨‍💻 When to Use Trigger.isExecuting


  • You have shared handler methods used by both triggers and controllers

  • You want to prevent duplicate operations (e.g., in recursive trigger scenarios)

  • You’re writing test classes and want to simulate trigger vs. non-trigger logic

  • You want a clean logging strategy that varies by context

  • You’re building reusable utilities that serve both declarative and programmatic use cases

🎥 Learn Visually – YouTube Playlist Included

As always, we’ve included a YouTube playlist to help you understand how Trigger.isExecuting works in practice. You’ll see examples of when the value returns true or false, and how your code behavior can change accordingly.

Solution:

public class AccountHandler{ public Boolean
        handleAccount(List<Account> accList){

            System.debug(‘Trigger is executing : ‘ + Trigger.isExecuting);
            if(Trigger.isExecuting)
                             //do whatever you want to do as part of the trigger invocation
            }
            else{
                        //do whatever you want to do if the call originated from a different                              context, such as from the controller.
            }
             return Trigger.isExecuting;
       }
}

Want to Apply As Content Writer?

Leave a Comment

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

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)