2

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();
mail.setTargetObjectIds(targetObjectIds);
mail.setTemplateID('00Xm0000000QEQ7');
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.

Tia.

1 Answer 1

2

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)
   targetObjectIds.add(cnt.Id);

Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(targetObjectIds);
1
  • It's pleasure for me to help you @user8737
    – highfive
    Jun 17, 2014 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .