Tag Archives: Trigger

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){
        handler.OnBeforeInsert(Trigger.new);
    }
    //After Insert
    else if(Trigger.isInsert && Trigger.isAfter){
        handler.OnAfterInsert(Trigger.new);
    }
    //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){
        handler.OnUndelete(Trigger.new);
    }
}

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){
        //EXECUTE BEFORE INSERT LOGIC
    }
    
    public void OnAfterInsert(List<Object> newObjects){
        //EXECUTE AFTER INSERT LOGIC
    }
    
    public void OnBeforeUpdate(List<Object> oldObjects, List<Object> updatedObjects, Map<Id, Object> ObjectMap){
        //BEFORE UPDATE LOGIC
    }
    
    public void OnAfterUpdate(List<Object> oldObjects, List<Object> updatedObjects, Map<Id, Object> ObjectMap){
        //AFTER UPDATE LOGIC
    }
    
    public void OnBeforeDelete(List<Object> ObjectsToDelete, Map<Id, Object> ObjectMap){
        //BEFORE DELETE LOGIC
    }
    
    public void OnAfterDelete(List<Object> deletedObjects, Map<Id, Object> ObjectMap){
        //AFTER DELETE LOGIC
    }
    
    public void OnUndelete(List<Object> restoredObjects){
        //AFTER UNDELETE LOGIC
    }
    
    public boolean IsTriggerContext{
        get{ return isExecuting;}
    }
}