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