Update Opportunity Description Based on Stage

This trigger updates an Opportunity’s Description to indicate its status: “Opp is Closed Lost,” “Opp is Closed Won,” or “Opp is Open,” based on its Stage.

Learn more about triggers here: YouTube playlist.

Solution:

trigger OpportunityTrigger on Opportunity (before update) {
                if(Trigger.isUpdate){ if(Trigger.isBefore){
                     OpportunityTriggerHandler.updateDesc(Trigger.New,
                     Trigger.oldMap);
             }
      }
}
     public class OpportunityTriggerHandler { public static void
                     updateDesc(List oppList,Map oldMap){
                                    for(Opportunity opp:oppList){
                                          if((oldMap==null)||(opp.StageName!=oldMap.get(opp.Id).Stage
                                                  Name)){ if(opp.StageName==’Closed Won’){                                                                                                          opp.Description=’Opportunity is
                                                  Closed won’;
                                     }else if(opp.StageName==’Closed Lost’){ opp.Description=’Opportunity is closed lost’;
                                     }else{ opp.Description=’Opportunity is open’;
                              }
                     }
              }
      }
}

 

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 :)