0

I need help. As per our requirment i have to send email to task owner if their task due date is passed and task is still open. I know that through workflow it is not achieveble. I have done a trick over here. I have created 2 check boxes name "second reminder" and third reminder" and update these field through workflows wth criteria Due date is greater or equal to TODAY once the field is updating a trigger is fire and send an email to task owner. Below is my code:

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.Second_Reminder__c != oldtask.Second_Reminder__c  || newTask.Third_reminder__c != oldTask.Third_reminder__c) {
            taskIdList = ownerIdTaskIdMap.get(newTask.ownerId);

            if(taskIdList == null) {
                taskIdList = new List<Id>();
            }

            taskIdList.add(newTask.Id);
            ownerIdTaskIdMap.put(newTask.ownerId, 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 = 'Please Update the tesk status'; // define your own subject

        //loop
        for(Id ownerId : ownerIdtaskIdMap.keySet()){
            userEmail = userMap.get(ownerId).Email;
            taskIdList = ownerIdtaskIdMap.get(ownerId);
            emailBody = 'Hi {0}, \n\n You have a task with open status. \n\n Please update it.' + 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);
    }
}

I want to me merge task fields like name subject and owner name in email body.

Please help

Regards Russell baker

0

if you check the Winter16 release notes on Process Builder regarding Schedule Multiple Groups of Actions, I'm fairly confident you should be able to do this without a trigger. But, as I've not tried it, I can't say for certain. I'll add that this would seem to be more of a management issue with holding employees accountable for their work.

Knowing that overdue tasks show up on someone's desktop every time they log-in would cause me to be concerned if there's such a big issue with Tasks that these employees need 2nd and 3rd reminders to get them completed, especially when their managers aren't being cc'd on these emails. But hey, if this is what your management wants you to do, I 'get' that it's your assignment to satisfy.

One concern I have about this trigger is that your workflow needs to fire in order to trigger it (something you might be able to add to process builder with it's latest revisions). So long as it does fire your trigger, the code below should work. You may need to add additional logic for Second_Reminder__c and Third_reminder__c fields in the string methods below if they contain due dates you want to convey or if you want to tell them 'this is your nth reminder'.

I've modified the main for loop in the center of your trigger to show you how this needs to be done. You can add any additional fields you wish following the same format. Before I forget, in order for the carriage returns you've included to work, I believe you need to specify a long text area field for the string variable that holds them.

     Map<Id, User> userMap = new Map<Id, user>([select id, Name, email FROM
        USER where id in: ownerIdTaskIdMap.keySet()]);
        //NOTE: Added `Name` to this query from higher up in your code


     sting subj = 'overdue task'; // additional string variable
     string emlb; // additional string variable


    //loop
    for(Id ownerId : ownerIdtaskIdMap.keySet()){

        //userEmail = userMap.get(ownerId).Email;
        // not needed, can get directly from map within this loop
        taskIdList = ownerIdtaskIdMap.get(ownerId);

        if(taskIdList.size() > 0){
        // test to see if a user has more than one task that's overdue

            for(task tsk : taskIdList){
               // compose your email body content
               emlb = 'Hi '+ userMap.get(OwnerId).Name +
               ',\n\n You have open tasks that are overdue.\n\n Please update Task:'
               + tsk.Id +'/n/n Subject: '+ tsk.Subject +'./n/n Type: '
               + tsk.Type +'./n/n Due Date: '+ tsk.ActivityDate +'./n/n/n';

            } // end for(task tsk : taskIdList)
               emailBody = emlb + '/n/n Thank you';
               // adds 'Thank you' to the list of tasks from the loop above  

        }else if(taskIdList.size() = 0){
            emailBody = 'Hi '+ userMap.get(OwnerId).Name +
            ',\n\n You have an open task that is overdue.\n\n    
            Please update '+ taskIdList.Id +'./n/n Subject: '
            + taskIdList.Subject +'./n/n Type: ' + taskIdList.Type +'./n/n Due Date: '+ taskIdList.ActivityDate +'./n/n/n Thank you';

        } // end if statements 

        } //end for(Id ownerId : ownerIdtaskIdMap

        //initiallize messaging method
        Messaging.SingleEmailMessage singleMail = new Messaging.SingleEmailMessage();
        singleMail.setToAddresses(new String[] {userMap.get(OwnerId).Email});
        singleMail.setSubject(subj);
        singleMail.setPlainTextBody(emailBody);
        emails.add(singleMail); //add mail
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.