Before Delete Trigger in Salesforce

by Sudipta Deb | Oct 7, 2014 | Apex, Salesforce, Trigger | 0 comments

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

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

Leave a Reply

Written by

Related Posts

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

Salesforce Cancels the “Permissions in Profiles” Retirement: What It Means for Admins and Architects

For the last few years, Salesforce administrators, architects, and security teams have been preparing for a major platform change: the retirement of permissions managed directly through Profiles. Salesforce had previously announced plans to move organizations toward a Permission Set–led security model, with enforcement expected to begin around the Spring ’26 timeframe.

read more...

0 Comments

Leave a Reply