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
kevin chongkevin chong 

help! question on whatId when sending email

Hi mates,
I got problems when sending emails, the code like following:
 
  Messaging.SingleEmailMessage mail=new Messaging.SingleEmailMessage();
  mail.setTemplateId(emailTemplateId);
  mail.setTargetObjectId(myContactId);
  mail.setWhatId(customObjectId);
  Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});
The problem is on customObjectId, i know the whatId can be set to some standard object and the custom objects. But there are some custom objects cannot set to whatId. For example, i got a RFQ object, its fields are almost the same with another Schedule object. When setting the RFQ id, it works fine. When setting the Schedule id, i got a exception like following:
 
System.EmailException: SendEmail failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Opportunity/Account ID: id value of incorrect type: a0070000009w4JMAAY: [WhatId]
 
So, is there a restriction of which custom object can be bound to the whatId field.
 
Thanks very much
Kevin
Best Answer chosen by Admin (Salesforce Developers) 
AkiTAkiT

I have found the same issue while setting my custom object correctly using the setWhatId.

 

However after hours of trying to found the bug in my code I have noticed that actually the message refers to Activity WhatId field !!

 

Because by default the setSaveAsActivity has a value true, which saves the emailing as an activity - so... your costom object needs to be allowed for activity tracking. This is a setting in the custom object page.

 

So if you have this issue, enable your custom object for activities or do not save the activity.

 

-AT