+ Start a Discussion
Monica FlahertyMonica Flaherty 

Trigger On Task

Is it possible to use a trigger on a task to send an email notification to the task creator when the status changes to Completed?  I'm very frustrated that this is not possible using a workflow rule, but am holding out hope that we can accomplish with a trigger. 

Saurabh DhobleSaurabh Dhoble

Yes, you can do that via a task-trigger. Below is the code. You can also do whole bunch of cool stuff like sending emails to whoever created the task, or to a group, adding details of the task in the email body etc. etc.

 

trigger taskTrigger on Task (after insert) {
	for(Task newTask : System.Trigger.new)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'monica.flaherty@gmail.com'}; 
        mail.setToAddresses(toAddresses);
        mail.setSubject('Hello');
        mail.setPlainTextBody('Your Case: ' + newTask.Id +' has been created.');
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

    }
}

 

SapamrmSapamrm

Hi Sauradh,

 

Thanks for this piece of code, I was looking for this as well.

 

But where in your code does it say that the trigger has to be executed as soon as the task status is set to completed?