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
Swamy PSwamy P 

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

Hello Folks,

I was not able to send an email. I have added WHATID as well, below is my code:
             mail.setToAddresses(ToUsersEmailIds);
             mail.setCCAddresses(ccUsersEmailIds);
             mail.setTemplateId(NOTIFICATION_EMAIL_TEMPLATE.Id);
             mail.setTargetObjectId(newContract.OwnerId);
             mail.setSaveAsActivity(false);
             mail.setWhatId(newContract.id);
             allMails.add(mail);
From some people i heard that i cannot add "setTargetObjectId" if i set "WhatId". But if i didn't set Targetobject Id it is throwing error, saying that required field missing.
Below is my exact error:
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: [whatId, 800Z0000000Cixc]: Class.ContractTriggerAfter.contractOwnerNotification: line 173, column 1

Please update me with your valuable solution,
Thnx in advance!!
Waqar Hussain SFWaqar Hussain SF
The error is because of setTargetObjectId, you can only use setWhatId() only when setTargetObjectId is contact.

According to documentation, If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
http://amitsalesforce.blogspot.in/2015/11/singleemailmessage-vs-massemailmessage.html

Sample code
public void SendEmail()
{
 List<contact> lstcon=[Select id from contact limit 2];
 List<Id> lstids= new List<Id>();
 for(Contact c:lstcon)
 {
  lstids.add(c.id);
 }
 EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];
 
 Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
 mail.setTargetObjectIds(lstIds);
 mail.setSenderDisplayName('System Admin');
 mail.setTemplateId(et.id);
 Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}

You can try below code as well
             mail.setTargetObjectId(newContract.id);
             mail.setSaveAsActivity(false);
             mail.setWhatId(newContract.OwnerId);

 
Swamy PSwamy P
Hello Vickey,
I'm sending email by using Contract object not the Contact object. While sending an email by apex it showing error messages that both are required.
Waqar Hussain SFWaqar Hussain SF
I dont think  setWhatId() for contract object is required. Can you comment below line, and chek again..
//mail.setWhatId(newContract.id);

 
Swamy PSwamy P
Hi Amit,
To merge the data into template i have to pass record id, for that i'm just using WhatId but whatever u have given it seems wrong. i.e, setWhatId as OwnerId and setTargetobjectId as record record ID. Even i tried vice versa, but still i'm facing the same error.
Will you advice further more.
 
Swamy PSwamy P
Hi Vickey,
Yeah setWhatId is optional but if i wanted to merge fields which are in template, i have to pass whatId as well.

So I'm struggling with that field only.