+ Start a Discussion
Anand@SAASAnand@SAAS 

Visualforce Email template + INVALID CROSS REFERENCE

Anyone know if VF Email templates run as "With Sharing"? I have a VF template that sends emails to contacts ie. recipientType="Contact"

I use Apex Email to send the email and set the "Target Object Ids" to the Contact Ids. However I get an "System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []".

Contact is "Public Read Only" but the Contact is not associated with an "Account".

If I use a regular email HTML template, and send an email using Apex Email, it goes through as expected.

Best Answer chosen by Admin (Salesforce Developers) 
AhmedPotAhmedPot

Well this error comes when you are assigning wrong ID to any object.I have also encountered this error few times. :)

 

for eg. when you have lookup to contact object. so you are assigning some Id to contact which doesn't exist or is referenced to some other object. Then it might throw an error.

 

I guess in this case you might be setting wrong target object Id to contacts.