function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sabrina Oliveira 3Sabrina Oliveira 3 

Sending emails with open tasks to their respective owners

I created a class scheduled to run once a week to let task owners know which tasks are open for them.
I am trying to compose the body of the email only with the open tasks of that specific user. With what I have so far, I am composing the email with all open tasks for all users. How can I do this?
public EmailTasksWeekly() {
        Map<Id, List<Task>> tasksByUser = new Map<Id, List<Task>>();
    for(Task taskRecord: [SELECT OwnerId, Id, Status, Subject, What.Name, ActivityDate FROM Task WHERE 
                          IsClosed = false AND Owner.IsActive = true]) {
        List<Task> userTasks = tasksByUser.get(taskRecord.OwnerId);
        if(userTasks == null) {
            tasksByUser.put(taskRecord.OwnerId, userTasks = new List<Task>());
        }
                              
        userTasks.add(taskRecord);
    }
tasks = [Select ID, OwnerId, Status, Subject, What.Name, ActivityDate FROM Task WHERE 
                 Status = 'Open' AND OwnerId = :tasksByUser.keySet()];
        }

 
Best Answer chosen by Sabrina Oliveira 3
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sabrina,

>> https://developer.salesforce.com/forums/?id=906F00000005KiXIAU

This link has a similar implementation that you can try checking once.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Sabrina,

>> https://developer.salesforce.com/forums/?id=906F00000005KiXIAU

This link has a similar implementation that you can try checking once.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Sabrina Oliveira 3Sabrina Oliveira 3
Thank you, Anutej. It helped me a lot!
Suraj Tripathi 47Suraj Tripathi 47

Hi,

global class SendEmailToDueDateTask implements Database.Batchable<sObject>  {

   Public List<Task> taskList{aet;get;}
     map<string,list<task>> userEmailTasklistmap = new map<string,list<task>>();
   public SendEmailToDueDateTask(){
   taskList=[SELECT ID,createddate,what.id,Owner.Email,OwnerId,owner.name,Status,ActivityDate,Subject from Task  Limit 50000];
   }

  
    
    global Database.QueryLocator start(Database.BatchableContext BC){
	return taskList;
     }
    
    global void execute(Database.BatchableContext BC, List<Task> scope){
        for(Task Tsk : scope){
            if(!userEmailTasklistmap.Containskey(tsk.owner.email)){
                userEmailTasklistmap.put(tsk.owner.email, new list<task>());
            }
            userEmailTasklistmap.get(tsk.owner.email).add(tsk);
            
          }  
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    
            for(string email : userEmailTasklistmap.keyset()){
                
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                list<string> toAddresses = new list<string>();
                toAddresses.add(email);
                mail.setToAddresses(toAddresses);
                mail.setSubject('Details of tasks due for today');                
                String username = userEmailTasklistmap.get(email)[0].owner.name;
                String htmlBody = '';
                
                htmlBody = '<table width="100%" border="0" cellspacing="0" cellpadding="8" align="center" bgcolor="#F7F7F7">'+
                            +'<tr>'+
                              +'<td style="font-size: 14px; font-weight: normal; font-family:Calibri;line-height: 18px; color: #333;"><br />'+
                                   +'<br />'+
                                    +'Dear '+username+',</td>'+
                            +'</tr>'+
                            +'<tr>'+
                                +'<td style="font-size: 14px; font-weight: normal; font-family:Calibri; line-height: 18px; color: #333;">You have Pending Task</td>'+
                            +'</tr>'+
                        +'</table>';
 
                htmlBody +=  '<table border="1" style="border-collapse: collapse"><tr><th>Related To</th><th>Subject</th><th>Created Date</th><th> Due Date</th></tr>';
                for(task tsk : userEmailTasklistmap.get(email)){
                    
                    String duedate = '';
                    if (tsk.ActivityDate != null)
                        duedate = tsk.ActivityDate.format();                    
                    else
                        duedate = '';
                    String Subject = tsk.subject;
                    datetime dt = tsk.createddate;
                    string createddate = dt.format('M/d/yyyy');
                    string what = tsk.what.id;
                    string link = URL.getSalesforceBaseUrl().toExternalForm()+'/'+ tsk.id; 
                    htmlBody += '<tr><td>' + what + '</td><td>' + Subject + '</td><td>' + createddate + '</td><td>' + duedate + '</td></tr>';                    
                }
                 htmlBody += '</table><br>';
                 mail.sethtmlBody(htmlBody);
                 mails.add(mail);                    
            }
             if(mails.size()>0)
             Messaging.sendEmail(mails);
    }
    global void finish(Database.BatchableContext BC){        
    }
}

Thank You