I need to pass a list of records into the email body in apex code. I am looking for the code sample or an Idea to start doing this. so far i am doing this.
Java script button on detail page. onclick calls a webservice method in a Apex class and sends an email with the list of records. All this code is part of my POC.
Button Code:
{!requireScript("/soap/ajax/20.0/connection.js")}
{!requireScript("/soap/ajax/20.0/apex.js")}
var retStr;
var accId = "{!Account.Id}";
alert('**** Acc Id'+accId)
retStr = sforce.apex.execute("MyWebServiceTest","makeContact",{accId:accId});
alert('The method returned: ' + retStr);
document.location = '/{!Account.Id}';
Class Code:
global class MyWebServiceTest{
webService static Id makeContact(Id accId) {
List<Contact> cntList=new List<Contact>();
cntList = [Select Id, Name from Contact where AccountId=:accId];
string template ='<html><head><style> body {font-family:verdana; font-size:13px;}</style></head><body>';
template += // Here I am expecting to add the cntList records to the template;
Messaging.Email[] emailList = new Messaging.Email[0];
for (id userId : userIds) {
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setTargetObjectId(userId);
message.setSaveAsActivity(false);
message.setSubject(subject);
message.setHtmlBody(body);
emailList.add(message);
}
}
if (!emailList.isEmpty())
Messaging.sendEmail(emailList, false);
}
}
<table>, <tr>
for every row, a<td> </td>
for every cell, a</tr>
at the end of each row of cells, and a closing</table>
tag. Its a matter of you working out what you want yours to look like isn't it?