Tuesday, October 7, 2014

Before Delete Trigger in Salesforce

Requirement:
Write a Trigger on Employee custom Object -> The requirement is that the trigger should prevent Delete operation on Employee custom Object if a particular field(i.e., canDelete__c) is not checked.
Solution:

trigger DeleteEmployeeTrigger on Employee__c (before delete) {
   if(Trigger.isDelete){
      for(Employee__c anEmployee : Trigger.old){
         if(!anEmployee.canDelete__c){
             anEmployee.addError(Cannot delete Employee:  + anEmployee.First_Name__c +   + anEmployee.Last_Name__c);
         }
      }
   }
}

0 comments:

Post a Comment