Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

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
     if(Trigger.isExecuting)
     {
        // Do Not Update Any contact
        System.debug(' $ $ NOT updating contacts');
     }
     else
     {
       // 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();
   demo.updateContact(Trigger.New);
}

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();
cc.updateContact(null);

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

$ $ updating contacts

$ $ return false // No trigger context

share|improve this answer
    
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 at 7:03
    
@RajeshShah Yes, because DML operation is firing a trigger which will result a trigger context. –  regalforce Feb 23 at 7:08
add comment

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 />
    <pre>{!log}</pre>
</apex:page>

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

share|improve this answer
    
Thats a wonderful example which clears all the doubts I had. Thanks for your time on this. –  RajeshShah Feb 24 at 4:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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