Monday, February 15, 2016

How to Implement Trigger Switch in Salesforce

Basic limitation of salesfroce we already know that we can't edit the apex code in Production .Recently we came acroos a situavtion where we need to stop the trigger in Production which is not working as expected.The common way to do this is get the trigger to sandbox and deactivate the same trigger in sandbox and then I have to push that same to prod.

But pushing the apex to PROD involves below things
   1.All the test classes will run we have to make sure that none will fail
   2.Code coverage percentage must be 75% or more

All this process is time consuming process and if the impact of that trigger on your business is critical meanwhile you will be losing more profit.So,The best way to stop the trigger immediately is implementing the trigger switch in your organisation

Trigger Switch :

Trigger switch is nothing but it's way to stop the execution of active trigger using the configuarion stuff.The best configuration feature available in salesforce to stop the trigger that we have to use Custom Setting or Custom Metadata Types.

How to Implement Trigger Switch using Custom Settings

1.Create a List custom setting with name Trigger Switch as shown in below image



2. Create two custom fields Active and Trigger Name on clicking on New in Custom Fields section.


3.Create Active field with data type as "Check Box" as shown in image



4.Create Trigger Name field with data type as "Text" as shown in image

5.Make this Trigger Name field as mandatory by selecting Required checkbox  as shown in image


Now custom setting is ready and I want to activate/deactivate one of the trigger in my org lets assume the trigger name is AccountTrigger from Custom settings without touching the active check box at the trigger level.To control this first we need to create a record for that trigger in Custom Setting.

6. Goto Custom Setting--->Click on Manage and give below details
            
            Name              : Account Trigger
            Trigger Name : AccountTrigger
            Active             : Checked

after saving the record it will looks like below            



7. To activate or to deactivate the trigger from custom settings we need to add the below code snippet at the starting of the trigger.

//Mute Trigger using  Custom Setting 
CustomSettingName switchVar = CustomSettingName.getInstance('TriggerName');
if(switchVar != NULL && 'False'.equalsIgnorecase(String.valueOf(switchVar.isActive__c))){ 
       return;
}

AccountTrigger before adding Switch


trigger AccountTrigger on Account (after update,after insert, before insert, before update) {
   
   //After Update Logic
   if (Trigger.isAfter && Trigger.isUpdate) {
    
        AccountUtility.callAfterUpdate(Trigger.New);
        
   } 
   
   //Before Insert or before Update Logic
    if (Trigger.isBefore &&  (Trigger.isUpdate || Trigger.isInsert) ) {
       AccountUtility.callBeforeUpsert(Trigger.New);
        
    }
    
   //After Insert Logic
   if(trigger.isAfter && trigger.isInsert)
   {  
       AccountUtility.callafterinsert(Trigger.New);
   }
    
}

AccountTrigger After adding Switch


trigger AccountTrigger on Account (after update,after insert, before insert, before update) {
   //Mute Trigger using  Custom Setting 
   TriggerSwitch__c switchVar = TriggerSwitch__c.getInstance('AccountTrigger');
   if(switchVar != NULL && 'False'.equalsIgnorecase(String.valueOf(switchVar.isActive__c)))   { 
           return;
    }
    
   //After Update Logic
   if (Trigger.isAfter && Trigger.isUpdate) {
    
        AccountUtility.callAfterUpdate(Trigger.New);
        
   } 
   
   //Before Insert or before Update Logic
    if (Trigger.isBefore &&  (Trigger.isUpdate || Trigger.isInsert) ) {
       AccountUtility.callBeforeUpsert(Trigger.New);
        
    }
    
   //After Insert Logic
   if(trigger.isAfter && trigger.isInsert)
   {  
       AccountUtility.callafterinsert(Trigger.New);
   }
    
}

How to Activate or Deactivate Trigger


To activate or deactivate the trigger from custom setting first add the trigger switch to each and every trigger in your organisation or to few triggers choice is yours.If you want to activate just go to the particular trigger related record in custom setting and check the active checkbox. to deactivate simply go to the trigger related record and unchecked the active checkbox .

Enjoy....!

No comments:

Post a Comment