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
Ganesh Babu 8Ganesh Babu 8 

setWhatId in send email causing "First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY" error

Hi All,

I am trying to send an email setting an Order record id in setWhatId method. It is throwing error "First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY". 
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

list<String> toaddress = new list<String>{'gbabu.tg@xxx.com'};

mail.setToAddresses(toaddress);
mail.setTemplateId(emailtemplt.Id);
mail.setTargetObjectId(Id.valueof(uId));
//If it comment below code, it is working.
mail.setWhatId(Id.valueof('8018E000000ghha'));

mail.setSaveAsActivity(false);

mails.add(mail);
Messaging.sendEmail(mails);
If I comment that particular line, it is wokring fine. As per the document setWhatId should accept Order record id, but it is throwing error. 
Please help me to find the exact issue.

Thanks in Advance!