User Story
As a user, I want to be prevented from deleting any Opportunity whose Stage Name is Closed Won to avoid losing important data. When I attempt to delete an Opportunity with a Stage Name of Closed Won, the system should display a validation message informing me that I cannot delete this record. This will help ensure that all closed deals and associated information are properly maintained and easily accessible in the future.
Solution 1
trigger OppTrigger on Opportunity (before delete) {
for(Opportunity opp : Trigger.old){
if(opp.StageName=='Closed Won'){
opp.addError('Closed won opportunities can not be deleted');
}
}
}
Solution 2
This trigger will only run before delete and check if the Stage Name of the opportunity being deleted is Closed Won. If so, it will prevent the deletion by throwing an error message. All other trigger events will not execute any logic.
trigger OppTrigger on Opportunity (before insert, after insert, before update, after update, before delete, after delete, undelete) {
if(Trigger.isBefore && Trigger.isDelete) {
for(Opportunity opp : Trigger.old){
if(opp.StageName=='Closed Won'){
opp.addError('Closed won opportunities cannot be deleted');
}
}
}
}
Video
Video does not exists.