2

This appears to be a simple use case that I can't figure out.

I want to send email to a list of UserIDs based on an HTML or VisualForce email template. The email template could look something like this:

Dear User,

Please have a look at the following Data Extract Request:

{!Data_Extract_Request__c.Request_Title__c}

Naturally, this template needs a reference to a Data Extract Request object. However, I have no idea how to do this with Apex. I was expecting something like the following in Apex:

Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();
email.setTargetObjectIds(userIds);
email.setWhatIds(dataExtractRequestIds); //Apparently, I can't do this
email.setTemplateId(templateId);
email.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.MassEmailMessage[] {email});

Unfortunately, it looks like there is no way to set to pass a custom object to an email template through apex. Is there a workaround to this problem? Or is there something I'm missing?

EDIT:

I have tried doing this through SingleMailMessage as well. The code is below:

List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
for (integer i=0; i<whatIds.size(); i++) {
  Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
  email.setTargetObjectId(targetObjectIds[i]);
  email.setTreatTargetObjectAsRecipient(true);
  email.setWhatId(whatIds[i]);
  email.setTemplateId(templateId);
  email.setSaveAsActivity(false);
  emailsToSend.add(email);
}
Messaging.sendEmail(emailsToSend);

I get the following exception:

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.

5

You can do this by adding the messages, indivudually, to a list

List<Messaging. MassEmailMessage > messagesToSend = new List<Messaging. MassEmailMessage>();

        for (Id objectId : dataExtractRequestIds) {
            Messaging. MassEmailMessage mail = new Messaging. MassEmailMessage();

            mail.setTargetObjectIds(userIds);
            mail.setTreatTargetObjectAsRecipient(true);

            mail.setTemplateId(templateId);
            mail.setwhatId(objectId);
            mail.setSaveAsActivity(false);

            messagesToSend.adD(mail);
        }

        Messaging.sendEmail(messagesToSend);

You can absolutely pass a custom object.

a) Ensure your header looks like the one I put below (change the subject, of course)

<messaging:emailTemplate subject="the Sibject" recipientType="User" relatedToType="Data_Extract_Request__c">

b) Then, on your merge fields, use this notation

{!RelatedTo.Request_Title__c}
  • That's interesting. I'll test this and let you know. – Mehdi Maujood Apr 18 '16 at 23:16
  • That did indeed execute. And my email was successfully sent. But unfortunately, the merge fields are empty. Why might that be? – Mehdi Maujood Apr 19 '16 at 21:40
  • I am assuming you checked that the ObjectId points to the correct object? If so, please update your original post with the entire template (include the headers)... – Sebastian Kessel Apr 19 '16 at 21:51
  • The empty merge fields were a fault on my part .. I had missed setting WhatId at all. I have updated the question with the current problem I'm facing. – Mehdi Maujood Apr 19 '16 at 22:56
  • it is possible to do what you need. I am updating my sample – Sebastian Kessel Apr 19 '16 at 22:58
0

Using information from Sebastian Kessel's answer, I was able to do this with the following code:

List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
for (integer i=0; i<whatIds.size(); i++) {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setTargetObjectId(targetObjectIds[i]);
    email.setTreatTargetObjectAsRecipient(true);
    email.setWhatId(whatIds[i]);
    email.setTemplateId(templateId);
    email.setSaveAsActivity(false);
    emailsToSend.add(email);
}

Messaging.sendEmail(emailsToSend);

The important point is that TargetObjectIds have to be contacts for WhatId to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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