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
jwhartfieldjwhartfield 

How can I find the Attachments for an EmailTemplate using APEX/SOQL?

I am trying to display to the user the attachments that are associated with an EmailTemplate object that they pick from a list.  According to the documentation, EmailTemplate attachments are stored in the Documents table and paired with their template by using the DocumentAttachmentMap.  But when I query the DocumentAttachmentMap, it is completely empty.  I have some EmailTemplates that I built with Attachments, so I expected to see some records in there.

 

Does anyone know where to find these Attachments or some caveat to displaying the records in the DocumentAttachmentMap table?

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

There are 2 ways for you to add an Attachment to an Email Template and the way you programatically access the attachment depends on which way you choose.

You can either attach any file from your local computer (which is what you did) or click on the 'Search in Documents' link in the attachment pop-up and attach a file from the Documents repository of Salesforce. The DocumentAttachmentMap junction object will only have an entry for the latter case. 

If you simply attached a file from your local computer, that attachment is stored in the 'Attachment' object in Salesforce. So you can query the Attachment object directly to get access to Email Attachments that were picked from your local computer.

 

For e.g. the following SOQL query in Apex would work:

 

EmailTemplate e = [select id from EmailTemplate where developerName = '<specify your template API name>' limit 1];

Attachment[] a = [ContentType, Body From Attachment where parentId = :e.Id];

All Answers

forecast_is_cloudyforecast_is_cloudy

There are 2 ways for you to add an Attachment to an Email Template and the way you programatically access the attachment depends on which way you choose.

You can either attach any file from your local computer (which is what you did) or click on the 'Search in Documents' link in the attachment pop-up and attach a file from the Documents repository of Salesforce. The DocumentAttachmentMap junction object will only have an entry for the latter case. 

If you simply attached a file from your local computer, that attachment is stored in the 'Attachment' object in Salesforce. So you can query the Attachment object directly to get access to Email Attachments that were picked from your local computer.

 

For e.g. the following SOQL query in Apex would work:

 

EmailTemplate e = [select id from EmailTemplate where developerName = '<specify your template API name>' limit 1];

Attachment[] a = [ContentType, Body From Attachment where parentId = :e.Id];

This was selected as the best answer
jwhartfieldjwhartfield

Thank you, that worked perfectly.  The reason I was not seeing any records was because I had attached files from my local computer.  When I looked in the Attachments table for them they were in there as you described.