I am trying to send an email with Apex code, and I have an ID to Set of Emails map called:


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.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.

  • Are you sure that id_email_map is exactly same as you described above? Jun 10 '14 at 10:41
  • Yes, actually I noticed that the toAddresses do have the right amount of email addresses. The only problem now is, even if no email address is contained in toAddresses, then the size is 1 anyway.
    – user8737
    Jun 10 '14 at 10:47
  • There must be empty string in your map and that is why its its showing size as one Jun 10 '14 at 10:51
  • Try debugging your code. Adding 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: salesforce.stackexchange.com/questions/36582/…
    – BarCotter
    Jun 10 '14 at 11:39

I just wrote slimier code and it shows correct size:

Map<String, List<String>> id_email_map = new map<String, List<String>>();
List<String> l = new List <String> ();
List<String> toAddresses = new List <String> (id_email_map.get('1'));

This means that there is issue with your variable id_email_map.

Check the contains of id_email_map

For empty string in list it will show size as one:

Map<String, List<String>> id_email_map = new map<String, List<String>>();
List<String> l = new List <String> ();
List<String> toAddresses = new List <String> (id_email_map.get('1'));
System.debug(toAddresses.size());//Shows size 0

l.add(''); //empty string
toAddresses = new List <String> (id_email_map.get('1'));
System.debug(toAddresses.size()); //Shows size 1
  • Yes, thank you very much. I did add an empty string and it showed up as 1. I now understand what the problem is and the above code is actually correct.
    – user8737
    Jun 10 '14 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.