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