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.
This article answers the question: how do you notify an Opportunity owner's manager by email automatically when a deal is marked "Closed Won"?
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.
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);
}
}
}
}
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.
000386016

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.