Restrict Opportunity Deletion Based on Stage

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

Want to Apply As Content Writer?

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

Let's get you started!

Interested in writing Salesforce Content?

Fill in this form and we will get in touch with you :)