I am trying to send email to Contact through Apex class with SingleEmailMessage for Event assigned/updated for him, so used:

SetWhatID = Event.id;

It is failing to send email with below exception:

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id

Can anyone help to understand the cause of this Exception? Please find detailed code below:

email.setWhatId(objEvent.id); // Event Id
email.setTemplateId(emailTemplate.id); // template Id with merged fields of Event
email.setToAddresses(new String[] {toAddress});
//Sending confirmtation mail
Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});
  • Please share the code in detail. – Nebula May 28 '15 at 8:03
  • Your email template is of which type text/vsiualforce. If not visualforce, try making it vf template and then try removing whatid code line. – Nebula May 28 '15 at 9:35
  • Yep!! it is visualforce template, after commenting What ID line, email is generated but blank values, – Priyanka Kajawe May 28 '15 at 9:53
  • What values you have given in recipientType & relatedToType attributes in template? – Nebula May 28 '15 at 10:02
  • recipientType = contact & relatedToType = Event – Priyanka Kajawe May 28 '15 at 12:01

When emails are saved as activities, a Task is created.

The WhatId on a Task cannot be an Event.

Even if you're using SaveAsActivity = false


I think that your problem is email.setWhatId(objEvent.id);

Can you try without there line?

Best regards.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.