User Story
As a Salesforce administrator, I want to prevent anyone from deleting a closed Opportunity, except for users with a System Administrator profile. This will ensure that valuable data is not lost and prevent accidental deletions
Code
Apex Trigger
trigger PreventClosedOpportunityDeletion on Opportunity (before delete) {
Id adminProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id;
for (Opportunity opp : Trigger.old) {
if (opp.isClosed && UserInfo.getProfileId() != adminProfileId) {
opp.addError('Only users with System Administrator profile can delete a Closed Opportunity.');
}
}
}