Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to send a massEmailMessage and I have the following code:

List <Id> targetObjectIds = new List <Id> ();

targetObjectIds = [SELECT Id FROM Contact];
System.debug('TargetObjects: ' + targetObjectIds);

Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});

When I run this code, then I get the following error:

Save error: Illegal assignment from LIST<Contact> to LIST<Id>

So I tried changing my code to:

List <Contact> targetObjectIds = new List <Contact> ();

But then I will get the following error:

Saver error: Method does not exist or incorrect signature: [Messaging.MassEmailMessage].setTargetObjectIds(LIST<Contact>)

I know that the method needs to take a list of Ids, but I am not sure how to get a list of Ids from the contact object.


share|improve this question
up vote 2 down vote accepted

Just populate a List with the retrieved data and use it for your setTargetObjectIds method.

List <Id> targetObjectIds = new List <Id> ();
List <Contact> targetObjects = new List <Contact> ();

targetObjects = [SELECT Id FROM Contact];
for(Contact cnt : targetObjects)

Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
share|improve this answer
Thanks a lot - that works! – user8737 Jun 17 '14 at 9:56
It's pleasure for me to help you @user8737 – highfive Jun 17 '14 at 9:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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