• Amber Wan
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
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>();
            }
            
            taskIdList.add(newTask.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
        
        //loop
        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}); 
            singleMail.setSubject(emailSubject); 
            singleMail.setPlainTextBody(emailBody); 
            emails.add(singleMail); //add mail
        }

        //send mail
        Messaging.sendEmail(emails);
    }

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:
(Task.Link)


Can anyone help to answer my question? 

Thank you,
Amber
 
Hi,
I'd like to set up the email notification for the task update. Once the Comments or Status field in the existing task has been updated by someone else, it can trigger the email notification to send to the task owner. Can it be done by the Apex Coding? 
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>();
            }
            
            taskIdList.add(newTask.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
        
        //loop
        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}); 
            singleMail.setSubject(emailSubject); 
            singleMail.setPlainTextBody(emailBody); 
            emails.add(singleMail); //add mail
        }

        //send mail
        Messaging.sendEmail(emails);
    }

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:
(Task.Link)


Can anyone help to answer my question? 

Thank you,
Amber
 
Hi,
I'd like to set up the email notification for the task update. Once the Comments or Status field in the existing task has been updated by someone else, it can trigger the email notification to send to the task owner. Can it be done by the Apex Coding?