Loading

Trigger to send email to Opportunity owner's manager

Fecha de publicación: Aug 18, 2026
Descripción

This article provides a sample Apex trigger that sends an email to an Opportunity owner's manager when the Opportunity's stage changes to "Closed Won."

The trigger fires on Opportunity update. It can be adapted for other requirements — for example, firing on insert instead of update, or checking a different field instead of Stage.

Solución

This article answers the question: how do you notify an Opportunity owner's manager by email automatically when a deal is marked "Closed Won"?

What this trigger does

The trigger below runs after an Opportunity is updated. When the Stage changes to "Closed Won," it looks up the Opportunity owner's manager and sends that manager an email notification.

Sample code

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 triggerNew , Map oppOldMap){
          Map opportunityMap = new Map();

          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 mails = new
List();
                    String body = 'won';
               for(Id oppId : opportunityMap.keySet()){
                    string email = opportunityMap.get(oppId);

                    Messaging.SingleEmailMessage mail = new
Messaging.SingleEmailMessage();
                    mail.setToAddresses(new List{email});
                    mail.setSubject('Automated email: Contact created');
                    mail.setPlainTextBody(body);
                    mails.add(mail);
               }
               if(mails.size() > 0){
                    Messaging.sendEmail(mails);
               }
               }
     }
}

How to customize it

This sample is written for the "after update" case, checking a Stage change to "Closed Won." To reuse it for a different trigger event or field, update the trigger event (e.g., "after insert") and change the condition inside opportunityHelper to match the field and value you want to check.

Número del artículo de conocimiento

000386016

 
Cargando
Salesforce Help | Article