+ Start a Discussion
viktorVviktorV 

Sending Mass Email

I'd like to create a scheduled mass email sending automatism.

My code is:

 

List<Lead> newLeads= [SELECT Id FROM Lead WHERE ... ];
List<Id> toAddresses = new List<Id>();
for (Lead l: newLeads){
     	toAddresses.add(l.Id);
}
Messaging.MassEmailMessage welcomeMail = new Messaging.MassEmailMessage();
welcomeMail_Italy.setTargetObjectIds(toAddresses);
welcomeMail_Italy.setTemplateId(' ... ');
welcomeMail_Italy.setReplyTo(' ... ');
welcomeMail_Italy.setSenderDisplayName(' ... ');

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

 I get the next error for the last line:

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

 

Can you help me pls?

 

Thanks

V

 

Best Answer chosen by Admin (Salesforce Developers) 
viktorVviktorV

the template id was wrong

All Answers

viktorVviktorV

'welcomeMail_Italy' in code equals 'welcomeMail'

viktorVviktorV

the template id was wrong

This was selected as the best answer