Allow Account Deletion Only for System Admins

This trigger ensures that only users with the System Administrator profile can delete Account records, adding a layer of security to critical data.

Explore more examples here: YouTube playlist.

Solution:

trigger AccountTrigger on Account (before delete)
             { if(Trigger.isDelete){ if(Trigger.isBefore){
                                                                      AccountTriggerHandler.checkProfileForDeletion(Trigger.old);
                        }
       }
}
public class AccountTriggerHandler{ public static void checkProfileForDeletion(List
          accList){

               Profile p = [SELECT Id FROM Profile WHERE Name = ‘System Administrator’];
               for(Account acc:accList){
               if(UserInfo.getProfileId() != p.Id){ acc.addError(‘Only System Administrator can delete
               Account’);
                             }
                }
       }
}

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)