+ Start a Discussion

Programmatically Sending Email Upon Task Creation

I have a function to programmatically create tasks:


    public static ID createTask(SObject taskObject, String subject, String priority,
                                String description, ID assignedToId) {
        Task theTask = new Task();
        theTask.WhatId = taskObject.Id;
        theTask.Subject = subject;
        theTask.Priority = priority;
        theTask.Description = description;
        theTask.OwnerId = assignedToId;
        insert theTask;
        return theTask.Id;


How do I also create a companion email that is sent immediately similar to the behavior of the 'Send Notification Email' checkbox on the new task UI page?






hi ... you have to include the code for sending the email manually upon the task creation, check whether the task has been created and then add the logic to send email heres the sample code below to send an email.

(in your case u should send the email to the owner of the task)


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'user@acme.com'};
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('New Case Created : ' + case.Id);
mail.setPlainTextBody('Your Case: ' + case.Id +' has been created');
mail.setHtmlBody('Your case:<b> ' + case.Id +' </b>has been created<p>'+
' View case <a href=https://na1.salesforce.com/'+case.Id+'>click here</a>');
// Send the email you have created.
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });





Thanks for trying to help.  I understand that manually creating an email is an option and appreciate your response.


My question though is this:  Is there any way to automatically create the companion email by simply setting a task parameter?  The task email created on the new task page is the result of simply checking a box.  What is this doing under the covers?  This email has a specific format which I know I can manually copy.  It would be nice if the UI capability is exposed in Apex.  Does anyone know if it is?









I don't believe so.  I beleive you have to create your own check box & then use the apex method above to send the email.


The Tasks product manager is out, but I'll send him a note & update this thread if he proves me wrong.


Thanks, John.


Hi all


Im faced with the same situation - so is there a way to set that "Send Notifcation Email" checkbox, through setting a flag, while creating tasks using Apex?