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
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com 

Can we iterate in SingleEmailMessage class

I am writing a batch class in which the last step is to send emails to assigned user , that these many " no.s" of tasks are needed to attended by him. So I want  send email in which all tasks are mentioned . What challenge i am facing is how to get all those tasks iterated in the " SingleEmailMessage" class .

 

The code below for now sends different emails for every different task . What I want is to consolidate all tasks into one email to that user.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	        for(integer k=0;k<accns.size();k++)
	        {
		        
		       	mail.setToAddresses(new String[] {'abhishek.pandey@sfclouds.com'});
				mail.setReplyTo('abhishek.pandey@sfclouds.com');
				mail.setSenderDisplayName('Saleforce Tasks');
				mail.setSubject(accns[k].Subject);
				mail.setPlainTextBody('Below is the task created for you as respectively');
				String sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm();
                //System.debug('Base URL: ' + sfdcBaseURL );
				mail.setHtmlBody('<br/><b>Status of task : ' + accns[k].Status +'<br/>Priority :'+accns[k].Priority +'<br/>Due Date :'+accns[k].ActivityDate+'</b><p>'+
			    '<b> To view your task : </b><a href='+sfdcBaseURL+'/'+accns[k].Id+'>click here.</a>');
				Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
	        }

 

 

I want below area to traversed with the list of all tasks assigned to user , when email is sent . That means no. of tasks is variable and changes for every other user.

 

mail.setHtmlBody('<br/><b>Status of task : ' + accns[k].Status +'<br/>Priority :'+accns[k].Priority +'<br/>Due Date :'+accns[k].ActivityDate+'</b><p>'+
			    '<b> To view your task : </b><a href='+sfdcBaseURL+'/'+accns[k].Id+'>click here.</a>');

Thanks......

 

Best Answer chosen by Admin (Salesforce Developers) 
Marko LamotMarko Lamot

why don't you iterate through tasks and generate proper body of all tasks;

 

string body = ''

for (iterate throught tasks)

{

body = body+'<br/><b>Status of task : ' + accns[k].Status +'<br/>Priority :'+accns[k].Priority +'<br/>Due Date :'+accns[k].ActivityDate+'</b><p> <br/><br/>'

}

 


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

...

mail.setHtmlBody(body);

....

 

 

All Answers

Marko LamotMarko Lamot

why don't you iterate through tasks and generate proper body of all tasks;

 

string body = ''

for (iterate throught tasks)

{

body = body+'<br/><b>Status of task : ' + accns[k].Status +'<br/>Priority :'+accns[k].Priority +'<br/>Due Date :'+accns[k].ActivityDate+'</b><p> <br/><br/>'

}

 


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

...

mail.setHtmlBody(body);

....

 

 
This was selected as the best answer
abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Thanks mark for suggestion just one more thing where should i write the iteration code ,

 

I hope before  the "singleemailmessage" class and what is this ----- string body = '' -------(I mean to say what  quotes signifies).

 

Thanks

Marko LamotMarko Lamot

 

yes, before the singleemailmessage function;  however it can be in the same class.

just create a body first iterating through your tasks;

end then put one singleemailmessage code using body you created before

 

body is just a string variable with which you will create html body;  and because you will create in iteration it has to be blank at the beggining , so  string body = ''

abhishek_pandey1989@yahoo.comabhishek_pandey1989@yahoo.com

Thanks it worked .....