+ Start a Discussion
Chinna SfdcChinna Sfdc 

Task Emails:

Hi Team,

Opprtunity owners and thier managers should get an email after task completion.we are trying to below trigger code, logic is working only for opportunity owner getting completion mail but not for their managers ,we need to send opoortunity owner and their managers also .Please help us for this.

Bit of code:

trigger TaskEmailNotification  on Task (after update ){
    Set<Id> ownerIds = new Set<Id>();
    for(Task tsk: Trigger.New){
        if(tsk.Status=='Completed'&&Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById ){
            ownerIds.add(tsk.CreatedById);      
        }       
    }   
    Map<Id,User> userMap = new Map<Id,User>([select Name,Email from User where Id in :ownerIds]);
    List<Messaging.SingleEmailMessage> emailMsglist=new List<Messaging.SingleEmailMessage>();    
    for(Task tsk : Trigger.New){
               if(tsk.Status=='Completed' && Trigger.oldMap.get(tsk.Id).Status != 'Completed' && tsk.OwnerId != tsk.CreatedById){
          
                 User theUser = userMap.get(tsk.CreatedById);
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses = new String[] {theUser.Email};
Thanks in Advance
SandhyaSandhya (Salesforce Developers) 
Hi Chinna,

Please see below article which has the code to send an email to opportunity manager.You can make changes according to your requirement.


Trigger to send email to Opportunity owner's manager

Knowledge Article Number000181297

DescriptionThis 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 clicking the "Clone" button on Opportunity record as per the requirement. It can be modified for other requirements changing the after insert or after update conditions.

 
Resolution:
 
​trigger managername on Opportunity (after update) { 
Opportunity i=[select ownerid, id,Owner.manager.email,Owner.manager.name from opportunity where id=:trigger.newMap.keySet()];
for (Opportunity o : Trigger.new) 
{ 
if(o.StageName=='Closed Won'){  
String userEmail = i.Owner.manager.email; 
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
String[] toAddresses = new String[] {userEmail}; 
mail.setToAddresses(toAddresses); 
mail.setSubject('Automated email: Contact created'); 
String body = 'won'; 
mail.setPlainTextBody(body); 
Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
} 
} 
}

Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya