function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
itsawsitsaws 

Please help me

Hi folks,

 

Please help me to understand the isExecuting() context variable in Apex trigger with an Example ?

 

Regards,

Sonu

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

"isExecuting" is another keyword to seggregate a piece of code while a transaction is going on which is initiated by a trigger.

 

When do we use it ?

Lets say we have a Utility class that does some job based on the params. This class is used by visualforce pages as well as a trigger. 

 

Example Scenario :

  • If the class is triggered by a trigger, a field on Account say isFromTrigger__c should be updated else isFromPage__c should be checked

Utility Class will look like

public class ExecutingDemo{
  public static Account checkAcc(Account acc){
    if(Trigger.isExecuting){
    	acc.isFromTrigger__c = TRUE;
    }
    else{
    	acc.isFromPage__c = TRUE;
    }
  }
}

 So when you call it from a trigger and visualforce it will behave differently based on the isExecuting.

 

 

 

All Answers

souvik9086souvik9086

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.

 

It just tells you whether you are right
now in the middle of executing a trigger.

 

In other words, if a web service inserts a record in the database, the insert trigger can't tell whether web service caused the trigger to execute or was it 
done from UI.

 

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

asish1989asish1989

HI

isExecuting context variable used for checking  if the current context for the Apex code is a trigger or not ?

I am sharing some code you can refer

trigger AddressUpdate on Address__c (before insert)
{
    if (Trigger.isExecuting) {
        System.debug('@@@@@@@@@@@INSIDE trigger########');
        // your logic
   }
}

 If this post helps you give kudos and mark it as solutions If it solves your question

 

Thanks

Avidev9Avidev9

"isExecuting" is another keyword to seggregate a piece of code while a transaction is going on which is initiated by a trigger.

 

When do we use it ?

Lets say we have a Utility class that does some job based on the params. This class is used by visualforce pages as well as a trigger. 

 

Example Scenario :

  • If the class is triggered by a trigger, a field on Account say isFromTrigger__c should be updated else isFromPage__c should be checked

Utility Class will look like

public class ExecutingDemo{
  public static Account checkAcc(Account acc){
    if(Trigger.isExecuting){
    	acc.isFromTrigger__c = TRUE;
    }
    else{
    	acc.isFromPage__c = TRUE;
    }
  }
}

 So when you call it from a trigger and visualforce it will behave differently based on the isExecuting.

 

 

 

This was selected as the best answer