Problems setting targetObjectIds

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.



Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x