User Story
As a Salesforce Admin, I want to ensure that only System Administrator profile users have the ability to delete an active Account. To achieve this, a trigger needs to be created that checks the user's profile before allowing an Account to be deleted.
Code
Apex Trigger
trigger PreventAccountDeletion on Account (before delete) {
Id sysAdminProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1].Id;
for (Account acc : Trigger.old) {
if (acc.Active__c == 'Yes' && UserInfo.getProfileId() != sysAdminProfileId) {
acc.addError('Only System Administrator profile users can delete Accounts.');
}
}
}