[SalesForce] Is it possible to send an Email Alert with a Workflow rule applied to a task object

We need to set an email alert action in a workflow rule applied to a task object and I have read on Salesforce documentation that this type of action is not allowed on task workflow.

Is there any way to do that? The default functionality allows to send an email when you create a task but it is not possible to change the email notification format and content. Has anybody the same problem? Thanks in advance.

Best Answer

I'm not sure if anyone can come up with a native workaround, I do not know of one. You can accomplish this with a trigger if you are comfortable with Apex though. I know it's not ideal but at least you know its possible.

Something like this should work. Don't really know your use case so this is very simplified and you will have to set the body, subject, recipients, etc. based on your use case. This should get you started though.

trigger myTaskTrigger on Task (after insert) {

    set<Id> oppIDs = new set<Id>();
    for(Task t : trigger.new){
         if(t.What.Type == 'Opportunity'){
               oppIDs.add(t.WhatId);
         }
    }

    map<Id, Opportunity> oppMap = new map<Id,Opportunity>([Select Id, Name From Opportunity Where Id In : oppIDs]); 

    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for(Task t : trigger.new){

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        // create email content
        String subject = 'My Subject for the task email alert';
        email.setSubject(subject);

        String body = 'Here is where I can se the body of the email';
        email.setPlainTextBody(body);

        // add recipients
        List<String> toAddresses = new List<String>();
        //Set your too addresses here based on task criteria
        email.setToAddresses(toAddresses);
    }

    if(!emails.isEmpty()){
        Messaging.sendEmail(emails);
    }
}
Related Topic