Loading

Trigger to send email to Opportunity owner's manager

Date de publication: Jun 26, 2023
Description
This article will provide the trigger to send an email to Opportunity owner's manager when the stage is "Closed Won". Currently, the trigger will work on updating the Opportunity record . It can be modified for other requirements changing the after insert or after update conditions.

 
Résolution
trigger managername on Opportunity(after update) {
    if(Trigger.isAfter){
       if(Trigger.isUpdate){
             for(Opportunity o: Trigger.new) {
                  ManagerNameHelper.opportunityHelper(Trigger.New,
                                                       Trigger.OldMap);
                }
        }
    }
}

public class managernameHelper{

public static void  opportunityHelper(List<Opportunity> triggerNew , Map<Id,Opportunity> oppOldMap){
          Map<Id,String> opportunityMap = new Map<Id,String>();

          for(Opportunity opp: triggerNew){
               String managerEmail = opp.Owner.Manager.Email;
             if(opp.StageName == 'Closed Won' &&
managerEmail!=oppOldMap.get(opp.Id).Owner.Manager.Email){
                opportunityMap.put(opp.Id,managerEmail);
             }
          }

             if(opportunityMap.size()>0){
                 List<Messaging.SingleEmailMessage> mails = new
List<Messaging.SingleEmailMessage>();
                 String body = 'won';
            for(Id oppId : opportunityMap.keySet()){
                string email = opportunityMap.get(oppId);

                Messaging.SingleEmailMessage mail = new
Messaging.SingleEmailMessage();
                mail.setToAddresses(new List<string>{email});
                mail.setSubject('Automated email: Contact created');
                mail.setPlainTextBody(body);
                mails.add(mail);
            }
            if(mails.size() > 0){
                Messaging.sendEmail(mails);
            }
            }
     }
}
Numéro d’article de la base de connaissances

000386016

 
Chargement
Salesforce Help | Article