Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

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

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

Your Answer

 
discard

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.