Tag Archives: Trigger

Usage of Trigger.IsExecuting in Salesforce

Trigger.isExecuting is to determine that your current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
Here is an example of Apex Class, this class can identify current context for the Apex code is a trigger using Trigger.isExecuting.

Apex Class:

public class AccountHandler
    public Boolean handleAccount(List<Account> accList){
        System.debug('Trigger is executing : ' + Trigger.isExecuting);
			//Do what ever you want to do as part of the trigger invocation
            //Do what ever you want to do if the call originated from different context, such as from controller.
        return Trigger.isExecuting;

Apex Trigger:

trigger AccountTrigger on Account (before insert, before update){
    AccountHandler handler = new AccountHandler();

How to deploy any trigger from sandbox to production?

In order to deploy a trigger from sandbox to production, you need to take care of some points:

  • At first I would like to request you all that before going for any deployment, make sure that there is no test failure or less than 75% code coverage in Production Instance. Because when we deploy any thing from sandbox to Production, “Run All Test” is performed automatically in Production. If there is any issue in Production then it will not let you deploy your new code.
  • If you are deploying any trigger in Production, then you must write a test class for that trigger with no Test failure and more than 75% code coverage.
  • Create deployment connection in sandbox.
  • Create Outbound change set in Sandbox and add your trigger and its associated test class in the change set.
  • Create Inbound change set in Production.
  • Add Change set components in Sandbox.
  • Upload the change set.
  • You can validate the change set in Production.
  • Finally deploy the change set in Production.

Hope this will help to deploy a trigger from sandbox to production.

Salesforce Trigger Handler Patterns

To move all code from Trigger to Apex Classes(Handler Classes) is one of the Salesforce Best Practices. Here is an example to write Trigger Handler class.

Apex Trigger:

trigger objectTrigger on Object (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
    objectTriggerHandler handler = new objectTriggerHandler();
    //Before Insert
    if(Trigger.isInsert && Trigger.isBefore){
    //After Insert
    else if(Trigger.isInsert && Trigger.isAfter){
    //Before Update
    else if(Trigger.isUpdate && Trigger.isBefore){
        handler.OnBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap);
    //After Update
    else if(Trigger.isUpdate && Trigger.isAfter){
        handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.newMap);
    //Before Delete
    else if(Trigger.isDelete && Trigger.isBefore){
        handler.OnBeforeDelete(Trigger.old, Trigger.oldMap);
    //After Delete
    else if(Trigger.isDelete && Trigger.isAfter){
        handler.OnAfterDelete(Trigger.old, Trigger.oldMap);
    //After Undelete
    else if(Trigger.isUnDelete){

Trigger Handler Apex Class:

public with sharing class ObjectTriggerHandler {
    private boolean isExecuting = false;
    public ObjectTriggerHandler(boolean isExecuting){
        this.isExecuting = isExecuting;
    public void OnBeforeInsert(List<Object> newObjects){
    public void OnAfterInsert(List<Object> newObjects){
    public void OnBeforeUpdate(List<Object> oldObjects, List<Object> updatedObjects, Map<Id, Object> ObjectMap){
    public void OnAfterUpdate(List<Object> oldObjects, List<Object> updatedObjects, Map<Id, Object> ObjectMap){
    public void OnBeforeDelete(List<Object> ObjectsToDelete, Map<Id, Object> ObjectMap){
    public void OnAfterDelete(List<Object> deletedObjects, Map<Id, Object> ObjectMap){
    public void OnUndelete(List<Object> restoredObjects){
    public boolean IsTriggerContext{
        get{ return isExecuting;}