function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
faceroy123faceroy123 

Email to Task Owners manager

Hi,

 

As I understand, email workflows are not permitted in SFDC.

 

I need to email a task owners manager (Task Owner > Manager) if a checkbox on the task = True.

 

Is there anyway to achieve this via apex?

 

Thanks

 

Adam

faceroy123faceroy123

Here is code I produced that sends an email to the case owner if the checkbox is set to true.

 

How do I modify this so it sends the same email to the manager of the task owner?

 

 

trigger WorkflowEmailFromOwner on Task (after update) { 
                if (trigger.new[0].escalated__c ==True ){ 
       Case cas = [select id, c.Owner.Email, case.Owner.Name  from Case c where id = :trigger.new[0].whatid]; 
                try{ 
        EmailTemplate etemp = [select id from EmailTemplate where 
Name=:'test1' and isactive=TRUE ]; 
        messaging.Singleemailmessage mail=new 
messaging.Singleemailmessage(); 
      //Save the Email in Activity History 
      mail.setSaveAsActivity(false); 
      //Change the Sender Display name to the Case Owner; 
        mail.setSenderDisplayName(cas.Owner.name); 
        //Change the Reply-To address to the case owner 
        //NOTE: the sending address will continue to be the default workflow user 
        mail.setReplyTo(cas.Owner.email); 
        mail.setTargetObjectId(cas.owner.id); 
        mail.setTemplateId(etemp.id); 
        try{ 
   messaging.sendEmail(new messaging.Singleemailmessage[]{mail}); 


}catch(DMLException e){ 


       system.debug('ERROR SENDING FIRST EMAIL:'+e.getDMLMessage(0)); 
  } 

        }catch(Exception Ex){ 


          trigger.new[0].addError('Errors occured: '+Ex); 


          system.debug(Ex); 
        } 


      } 



}