This trigger restricts Opportunity deletion if the Stage is “Closed Won” or “Closed Lost.” Only System Administrators can delete such Opportunities.
For additional learning, check this YouTube playlist.
Solution:
trigger OpportunityTrigger on Opportunity (before delete) {
if(Trigger.isDelete){ if(Trigger.isBefore){
OpportunityTriggerHandler.checkProfileForDeletion(Trigger.old);
}
}
}
public class OpportunityTriggerHandler { public static void
checkProfileForDeletion(List<Opportunity> oppList){
Profile p = [SELECT Id FROM Profile WHERE Name = ‘System
Administrator’]; for(Opportunity opp:oppList){ if(opp.StageName == ‘Closed
Won’ || opp.StageName ==
‘Closed Lost’) if(UserInfo.getProfileId() != p.Id){ opp.addError(‘Only
System administrator can delete opportunity’);
}
}
}
}