function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JeremyKraybillJeremyKraybill 

Email: Can't set WhatId when TargetObjectId is a User?

I am running into an issue in Apex outbound email. If I have an email message that is using a template, and that I am sending to a User (via setTargetObjectId), Apex won't let me set WhatId -- if I do, it gives me this error message

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

Is there any easy way around this limitation? Obviously via workflow rules I am able to send emails to users that have a "What" context sent (for instance, send an email to Opportunity owner when stage change X occurs). Seems like a big limitation of the Apex email service to not allow this.

For now, I am intending to set up temporary Contact objects for Users who do not have them, just so I can send them email.

Here is the code that is hitting this error. "UserId" is the ID of a User object and "WhatId" is the ID of an Opportunity.

Code:
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
EmailTemplate template = [select Id from EmailTemplate where DeveloperName = :emailTemplateUniqueName];
mail.setTemplateId(template.Id);
mail.setReplyTo('no-reply@mycompany.com');
mail.setSenderDisplayName('Salesforce');
mail.setTargetObjectId(userId);
mail.setWhatId(whatId);
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 
Any pointers much appreciated. If (as I suspect) this is just a straight Apex issue and cannot be done, I'll open an Idea to fix it.

Jeremy Kraybill
Austin, TX
Rasmus MenckeRasmus Mencke
You can do this for Visualforce email templates - it works with
targetObjectId = user
whatId = Opportunity
TemplateId = Visualforce Template

You can not use it with the regular templates.
cmarz_1cmarz_1
I'm running into the same issue but I'm in a trigger so a VF page won't work for me. 
sparkysparky

I have run into this as well, also in a trigger.

 

I've posted an Idea here, please vote: 

SF idea

Kinik, GorkyKinik, Gorky

if there are no fields from user or contact in the template, i use a workaround for this situation like this.

 

Create a secret generic contact.

Update email of the contact with the email of the user just before sending the template.

Then set targetobjectid to this contact and send mail.

Manan_ShahManan_Shah

hi

I am facing the same problem

whatId is an INVALID_ID_FIELD for sending mails.

If you find any solution, then pls tell me......

 

Thanks

Manan

colemabcolemab

I have a write up about this (and more) here but basically if you remove the recipientType from the <messaging:emailTemplate> tag in the visualforce email template, you can then use the targetobjectid for a user while setting the whatid.

Yogesh BhosaleYogesh Bhosale
I was facing the Problem of INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.

Then the solution appled was , For following function 

mail.setTargetObjectId(userId);

Instead of passing userID create a Contact and pass the Contact ID . If the Object is custom Object its data will not get resolved if you use userid it has to be Contact ID.