+ Start a Discussion
Amber WanAmber Wan 

Notify any task update to task creator

Hi there,

I am trying to add the following apex triggers to notify any task update to the task creator. It does work but I would like to make the email body more details so that when the task creator receive the notification, he can know which task exactly has been updated. 

Here is the apex triggers:
trigger NotifyTaskOwner on Task (After Update) {

    List<Id> taskIdList;    
    Map<Id, List<Id>> ownerIdTaskIdMap = new Map<Id, List<Id>>();

    Task oldTask;
    for(Task newTask : Trigger.new){
        oldTask = Trigger.oldMap.get(newTask.id);
        if(newTask.status != oldtask.status  || newTask.Description != oldTask.Description) {
            taskIdList = ownerIdTaskIdMap.get(newTask.CreatedById);
            if(taskIdList == null) {
                taskIdList = new List<Id>();
            ownerIdTaskIdMap.put(newTask.CreatedById, taskIdList);
    if(!ownerIdTaskIdMap.isEmpty()) {
        Map<Id, User> userMap = new Map<Id, user>([select id, email from user where id in: ownerIdTaskIdMap.keySet()]);
        //list of emails
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        String userEmail, emailBody;
        String emailSubject = 'The task created by you has been updated'; // define your own subject
        for(Id CreatedById : ownerIdtaskIdMap.keySet()){
            userEmail = userMap.get(CreatedById).Email;
            taskIdList = ownerIdtaskIdMap.get(CreatedById);
            emailBody = 'The task created by you has been updated ' + taskIdList;
            for(Id tskId : taskIdList){
                // compose your email body content
            Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();   //initiallize messaging method
            singleMail.setToAddresses(new String[] {userEmail}); 
            emails.add(singleMail); //add mail

        //send mail

This is the example for the email body message after the apex triggers have been fired.
Email body (00T8A00000319vXUAQ)

I would like to change the email body message to:
Your task (Task.Subject) has been updated by (Task.LastModifyBy).
For more details, please click on the following link:

Can anyone help to answer my question? 

Thank you,

Replace the singleMail.setPlainTextBody into below code
// use the html body to set the content of the body
singleMail.setHtmlBody('Your task:<b> ' + Task.Subject +' </b>has been updated by'+Task.LastModifyBy +'\n To view your case <a href=https://login.salesforce.com/'+Task.Id+'>click here.</a>');

Amber WanAmber Wan

It doesn't work. Here is what I got in the email body message:

Your task: Subject created onCreatedDate To view your case click here.

When I click on the link. It directs to the following address:

Can you please take a look into the appex codes?

Thank you,