4

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.setTargetObjectId(loggedInContactId);
email.setWhatId(objEvent.id); // Event Id
email.setSaveAsActivity(false);
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});
5
  • Please share the code in detail.
    – Nebula
    Commented May 28, 2015 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
    Commented May 28, 2015 at 9:35
  • Yep!! it is visualforce template, after commenting What ID line, email is generated but blank values, Commented May 28, 2015 at 9:53
  • What values you have given in recipientType & relatedToType attributes in template?
    – Nebula
    Commented May 28, 2015 at 10:02
  • recipientType = contact & relatedToType = Event Commented May 28, 2015 at 12:01

2 Answers 2

2

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

0

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

Can you try without there line?

Best regards.

4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .