I am trying to send an email with Apex code, and I have an ID to Set of Emails map called:
id_email_map
and it looks something like:
ID1 - {email1@test.com, email2@test.com}
ID2 - {}
ID3 - {email1@test.com}
In my code I now want to send one specific email to all the addresses in ID1, no email to ID2 (because it doesn't have an email address), and one specific email to all addresses of ID3.
In my code I write:
for (Id id : id_email_map.keySet())
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//get email addresses for specific id
//I think here is my first problem, because when I debug
//the size of toAddresses is always 1
List<String> toAddresses = new List <String> (id_email_map.get(id));
//and the second problem is, since toAddresses.size is always 1,
// the if statement doesnt catch the null case
if (toAddresses.size() >= 1)
{
mail.setToAddresses(toAddresses);
mail.setReplyTo('test@test.com');
mail.setSenderDisplayName('Ms Reena');
mail.setSubject('Test Email');
mail.setPlainTextBody('This is a test email');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Any help would be greatly appreciated. Thanks, Reena.
System.debug(toAddresses);will print out the contents and you should be able to figure out what's going on. See this question for more info: http://salesforce.stackexchange.com/questions/36582/how-do-i-start-to-debug-my-own-apex-code – BarCotter Jun 10 '14 at 11:39