You need to sign in to do that
Don't have an account?

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.
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
"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 });
Jeremy Kraybill
Austin, TX
targetObjectId = user
whatId = Opportunity
TemplateId = Visualforce Template
You can not use it with the regular templates.
I have run into this as well, also in a trigger.
I've posted an Idea here, please vote:
SF idea
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.
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
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.
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.