Loading

Trigger to send email to Opportunity owner's manager

Publiceringsdatum: Jun 26, 2023
Beskrivning
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.

 
Lösning
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);
            }
            }
     }
}
Knowledge-artikelnummer

000386016

 
Laddar
Salesforce Help | Article