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!’);|
}
}
}
}