I want to send email to user about his open tasks. Each user will have more than one task.
I have a set of taskIds and querying all tasks based on these IDs.
How do I get email address of task owners and send email with list of tasks for each owner.
//Task User Map Map<ID, List<Task>> tasksByUserMap = new Map<ID, List<Task>>(); // Query all tasks List<Task> lstReminderTasks = [SELECT ID, Subject, ActivityDate, WhatId, OwnerId, Owner.Email FROM Task WHERE ID IN: taskIds AND Status = 'Open' ]; Here's a sample data for the above query:
Owners XYZ and ABC have two tasks each.
//Build Task Owner Map from the list for(Task task: lstReminderTasks){ List<Task> userTasks = tasksByUser.get(taskRecord.OwnerId); if(userTasks == null) { tasksByUserMap.put(task.OwnerId, userTasks = new List<Task>()); } userTasks.add(task); } Messaging.SingleEmailMessage mail = = new Messaging.SingleEmailMessage(); OrgWideEmailAddress owe = [SELECT ID,DisplayName,Address FROM OrgWideEmailAddress WHERE DisplayName = 'Support']; // Build email content and To addresses if(!tasksByUserMap.isEmpty()) { String[] toaddress; htmlBody += 'Below tasks are due : ' + '</br> </br>'; htmlBody += '<table><tr><th>Task Subject</th><th>Due Date</th><th>Priority</th></tr>' for(Task t : lstReminderTasks ) { htmlBody += '<tr>'; htmlBody += '<td>'+ t.Subject +'</td>'; htmlBody += '<td>'+ t.ActivityDate +'</td>'; htmlBody += '<td>'+ t.Priority+'</td>'; htmlBody += '</tr>' mail.setOrgWideEmailAddressId(owe.ID); mail.setSaveAsActivity(false); mail.setToAddresses(toAddress); // How to get toAddress here ? mail.setHtmlBody(htmlBody ); } } I need help with -
- Refining my code to get the ToAddress based on owner ID
- Create a data structure / map to send email to owner regarding their tasks only.
