Restrict Case Deletion to System Admins

This trigger restricts the deletion of Case records to users with the System Administrator profile, ensuring that only authorized personnel can perform such actions.

Explore more triggers here: YouTube playlist.

Solution:

public class CaseTriggerHandler {
       public static void handleBeforeDelete (List<Case> oldRecords) {
             Id currentUserProfileId = UserInfo.getProfileId(); User Info.
             Id sysAdminProfileId = [SELECT Id, Name FROM Profile WHERE Name = ‘System
             Administrator’ LIMIT 1].Id;
             for (Case caseRec : oldRecords) {
             if (sysAdminProfileId != currentUserProfileId) {
                 caseRec.addError(‘You dont have the rights. Go away!’);|
              }
          }
   }
}

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 :)