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.
Apex 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;
- }
- }
Apex Trigger on Contact object
- trigger ContextCheckTrigger on Contact (before insert, before update)
- {
- TriggerContextDemo demo = new TriggerContextDemo();
- demo.updateContact(Trigger.New);
- }
$ $ 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
Thanks to StackExchange Community