function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Syed F RazaSyed F Raza 

Trigger to Prevent Deletion of Record

Hi All,

I have an existing trigger on a custom object called Student for insert and update events. I wanted to add a condition to restrict deletion of record(s) on object Student. The trigger code I wrote is as follows:

 
trigger TriggerOnStudent on Student__c (before insert, before update, before delete) {
  TriggerHandlerOnStudent.AddSalutation(trigger.new);
  TriggerHandlerOnStudent.AddCampus(trigger.new);
   TriggerHandlerOnStudent.GiveDiscount(Trigger.new); 
    TriggerHandlerOnStudent.doNotDeleteError(Trigger.old); 
    
      
}

The code of trigger handler class is as follows:
 
public class TriggerHandlerOnStudent {
  public static void AddSalutation(List<Student__c> stuList){
        for(Student__c stu:stuList)
        if(stu.Gender__c!=null && stu.Gender__c=='Male')
        stu.Name='Mr '+stu.Name;
    
        else if(stu.Gender__c!=null && stu.Gender__c=='Female')
        stu.Name='Miss '+stu.Name; 
        
    }   
     
      public static void AddCampus(List<Student__c> studList1){
        for(Student__c stu1 : StudList1)
            if(stu1.Campus__c==null)
            stu1.campus__c='Wayne';
        }     
    
    public static void GiveDiscount(List<Student__c> studList2){
        for (Student__c stu2:studList2){
            if(stu2.Fees__c != null && stu2.County__c=='LA County')
                stu2.Fees__c=stu2.Fees__c - (stu2.Fees__c*.1);
            else if (stu2.Fees__c != null && stu2.County__c=='Orange County')
                stu2.Fees__c = stu2.Fees__c - (stu2.Fees__c*.2);
            else if (stu2.Fees__c != null && stu2.County__c =='Riverside County')
                stu2.Fees__c = stu2.Fees__c - (stu2.Fees__c*.3);
        }
        
        }

    public static void doNotDeleteError(List<student__c> studList3) {
        
        for(Student__c stu3:StudList3){
        stu3.addError('You cannot delete this record');
    }
} 
    
}

Once I activated this trigger, this trigger is working on insert and update events but it does not work when I try to delete a record. Even though it does not allow me delete a record but it gives me an error that I do not have rights as user and I need to contact administrator.

I deactivated this trigger.

Then I wrote a seperate trigger on Student object to restrict deletion of records and this time it works. The code of trigger is as follows:
trigger TriggerOnStudent on Student__c (before delete) {

       for(Student__c student1: trigger.old){
           student1.adderror('You cannot delete this record');
       }
    
      
}

Any idea what was wrong with the original trigger I wrote.
Defintely it was not issue of permissions as:
  1. I am the administrator.
  2. Not with the second trigger, code is working perfect


 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Syed,

Please try modifying the code in trigger from below:
trigger TriggerOnStudent on Student__c (before insert, before update, before delete) {
  TriggerHandlerOnStudent.AddSalutation(trigger.new);
  TriggerHandlerOnStudent.AddCampus(trigger.new);
   TriggerHandlerOnStudent.GiveDiscount(Trigger.new); 
    TriggerHandlerOnStudent.doNotDeleteError(Trigger.old); 
    
      
}

to:
 
trigger TriggerOnStudent on Student__c (before insert, before update, before delete) {

  TriggerHandlerOnStudent.AddSalutation(trigger.new);
  TriggerHandlerOnStudent.AddCampus(trigger.new);
   TriggerHandlerOnStudent.GiveDiscount(Trigger.new); 
if(trigger.isbefore && trigger.isdelete)
{    TriggerHandlerOnStudent.doNotDeleteError(Trigger.old); }
}

this will make sure that the doNotDeleteError runs only on delete operation and not on insert and update operations.

Please do note that it is generally not considered as a best practice to have multiple triggers on single object and for more best practices you can check the below link:

>> https://developer.salesforce.com/forums/?id=906F0000000DBl8IAG

>> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Syed F RazaSyed F Raza
@Anutej

Thanks for the reply.

You have mentioned that it is not a good practice to have multiple triggers on one object. But I only have one trigger on this object.

 
ANUTEJANUTEJ (Salesforce Developers) 
I meant to say you need to have only one active trigger and also after making above changes did it help?
Suraj Tripathi 47Suraj Tripathi 47
Hi Syed F Raza,
Kindly find solution.
trigger TriggerOnStudent on Student__c (before insert, before update, before delete) {
    
    if(Trigger.isBefore && Trigger.isInsert){
        TriggerHandlerOnStudent.AddSalutation(trigger.new);
        TriggerHandlerOnStudent.AddCampus(trigger.new);
        
    }
    
    if(Trigger.isBefore && Trigger.isUpdate){
        TriggerHandlerOnStudent.GiveDiscount(Trigger.new); 
        
    }
    
    if(Trigger.isBefore && Trigger.isDelete){
        TriggerHandlerOnStudent.doNotDeleteError(Trigger.old); 
    }
    
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.