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

attachements on emailtemplate and emailsend() method generate INVALID_CROSS_REFERENCE_KEY,

in an apex future method launched by a trigger I am using SingleEmailMessage to send email messages to specific email addresses, using a Lightning EmailTemplate, following almost the same approach mentioned here: Trigger to send email with related attachments
All works great if the EmailTemplate has no attachment. Instead, if the same EmailTemplate has an attachment, then the SendEmail() command generates the following error:
SendEmail failed. First exception on row 0; first error:INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
It seems that SingleEmailMessage does not support EmailTemplates with attachments. Anyone had the same experience?
Please note that my org is with Summer '18 and that email settings related to EMAIL ATTACHMENTS are "Include as attachment up to 3 MB or as links if more". I noticed that if settings are "Always as links" then the error does not happen (but links added to the sent email will not work).
The same template sent manually via Lightning Email Composer will work, either with "Include as Attachment" or "Always as links settings."
My current conclusions are: * the Lighting composer does not just use the SingleEmailMessage.sendEmail command: it will do a lot of pre processing before, in order to manage attachments properly. This means for example create a proper public link for the attachment. * in order to send emails via apex, we'll need to re-implement the same logic.
Anyone has any idea about this / did it already?
Tyson KirkseyTyson Kirksey
Sebastiano, I ran across this same issue.  Did you ever find a workaround?