As per the documentation, trigger.isExecuting returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.

I am a bit confused by this. If VF page calls a controller function which makes an DML operation firing the trigger, what would be the value of isExecuting in that case?


In brief Trigger.isExecuting determines if the context is trigger or Visualforce. If it returns true context is trigger otherwise context is Visualforce or other. In your case Trigger.isExecuting will return true.

Simple ex:

Create a class:

public class TriggerContextDemo
  public Boolean updateContact(Contact[] conList)
     // Will not update contact if method is called in TriggerContext
        // Do Not Update Any contact
        System.debug(' $ $ NOT updating contacts');
       // update contacts
       System.debug(' $ $ updating contacts');
     System.debug(' $ $ return ' + Trigger.isExecuting);
     return Trigger.isExecuting;

Now Create a trigger on Contact

trigger ContextCheckTrigger on Contact (before insert, before update) {
   TriggerContextDemo demo = new TriggerContextDemo();

Now if any of Contact's record is updated or inserted by Visualforce Controller or manually it will fire a trigger and in the trigger instance method of TriggerContextDemo is called and result in System.debugs:

$ $ NOT updating contacts // Because Trigger.isExecuting is true

$ $ return true // Trigger Context exist

If we call the same class from Developer console's execute Anonymous like this:

TriggerContextDemo cc = new TriggerContextDemo();

In this case same class will result in system.debugs:

$ $ updating contacts

$ $ return false // No trigger context

  • So even though if the starting point of the execution was not a trigger (a visualforce controller in my case) which in turns results in a trigger execution, isExecuting will be true? – RajeshShah Feb 23 '14 at 7:03
  • 1
    @RajeshShah Yes, because DML operation is firing a trigger which will result a trigger context. – Ashwani Feb 23 '14 at 7:08

Building on Regal's answer, the value can be change within the same action. For example, if you have a visualforce action that performs DML within the action method Trigger.isExecuting will be false, but within triggers invoked by the DML this action causes Trigger.isExecuting will be true.

To demonstrate let's set up a class with a static string variable we can write to from the controller action and the trigger we invoke:

public class MyPageController{
    public static String log = '';

    public PageReference doSomeDML(){
        log += 'Starting doSomeDML(); - Trigger.isExecuting = '+Trigger.isExecuting+'\n';
        insert new Contact(LastName = 'Dummy contact');
        return null;

    public String getLog(){
        return log;

We'll also create a trigger on contact that writes to MyPageController.log:

trigger ContactLogTrigger on Contact (before insert) {
    System.debug('Log state is: '+ MyPageController.log);
    MyPageController.log += 'In trigger ContactLogTrigger - Trigger.isExecuting = '+Trigger.isExecuting+'\n';

And finally a page to display the results:

<apex:page controller="MyPageController" action="{!doSomeDML}" sidebar="false">
    Log for doSomeDML: <br />

If you run this page you'll see Trigger.isExecuting is different within the same transaction depending on if you're in a trigger or not:

enter image description here

  • Thats a wonderful example which clears all the doubts I had. Thanks for your time on this. – RajeshShah Feb 24 '14 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.