I am writing a batch job to send an email to a lead owner's manager. I have List<String> mgremails in which I have extracted only the email addresses of the manager.

List<User> mgr = [select Manager.email FROM User where Id in :leadOwnerId];

list<string> mgremails = new list<string>();
for(User u : mgr){

I now need String[] as parameter for the setToAddresses method of the Messaging class. How do I do this conversion?

  • I believe there are many question related to this one. Anyways, List<String> and String[] are treated as same in Salesforce. You don't need conversion. – Mahmood May 3 '17 at 19:40
  • Check this question related to the same topic under discussion and has good detail. – Mahmood May 3 '17 at 19:41
  • There is another one with the same question. – Mahmood May 3 '17 at 19:43
  • is my syntax correct ? mail.setToAddresses(mgremails); – ngc May 3 '17 at 19:44
  • Yup, your syntax looks fine to me. – Mahmood May 3 '17 at 19:44

They're the same. You can pass a List<String> instance in where String[] is expected, and vice versa.

Here's a simple script you can run in Execute Anonymous to prove it:

static void doStuff(List<String> input) { system.debug(input); }
static void doOtherStuff(String[] input) { system.debug(input); }

doStuff(new String[]{ 'a', 'b', 'c' });
doOtherStuff(new List<String> { 'x', 'y', 'z' });

From the comments, make sure you actually call setToAddresses on your SingleEmailMessage instance:

| improve this answer | |
  • Boy! You are following the comments and updating your answer quickly. Whew! :D – Mahmood May 3 '17 at 19:51
  • @ngc Never send emails in a loop! You only get 10 invocations. – Adrian Larson May 3 '17 at 19:56
  • @ngc Perhaps you should ask a separate question that is not a duplicate as indicated above, and lays out what you have done so far and the verbatim error message. Changing the context via comments isn't great for usability as far as the Stack Exchange format is concerned. – Adrian Larson May 3 '17 at 20:08

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