Loading

Trigger to send email to Opportunity owner's manager

Fecha de publicación: Jun 26, 2023
Descripción
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.

 
Solución
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);
            }
            }
     }
}
Número del artículo de conocimiento

000386016

 
Cargando
Salesforce Help | Article