Our customer service agents are sending emails to customers from cases through the standard "Send an Email" page where they select a template. Often they edit the text before sending.

We'd like to track which email templates are used in which cases. (Background: This is a preparation for automating parts of this. The idea is to train an AI to propose and eventually send the correct template for new cases based on the customer's message and some metadata. To create training data for the AI, we need to track which email templates are used in each case.)

My first idea:

Add new text fields on Email Message that hold the Email Template's id and the unique name (Salesforce does not allow lookups to Email Templates). Then I re-build the "Send an Email" page as a Visualforce page and store the template id and unique name on the new Email Message every time one is sent. I could then use a "cases with emails" report to get the data. Optionally store whether the email text was edited before sending.

  • Pro: Only adds custom fields on Email Message, so storage usage is not a problem.
  • Cons: Have to re-build a standard page that is used with high frequency.

I'd appreciate all comments. How would you go about tracking email template usage?

  • This all depends how you're generating the email templates, but if you're doing it through HTML / Visualforce, add a hidden id where you store the templateid, like <div id="templateid">00Y74000000NsvC</div>. Then add a trigger on before/after insert on EmailMessage where you parse the body looking for that templateid value then save it in some custom object. Jan 12, 2018 at 16:49
  • Thanks Caleb, that would work, but since we have literally thousands of email templates, this would create a lot of work updating all of them. So I will wait for other ideas.
    – georg w.
    Jan 15, 2018 at 7:16
  • You could run a script to update them all. Select HtmlValue from EmailTemplate. For loop through those objects, setting the HtmlValue += '<div id="templateid">00Y74000000NsvC</div>', then update the List<EmailTemplate>. It's not a pretty solution but it's feasible. Jan 16, 2018 at 19:04
  • I'd put something on the trigger to validate the ID in lots of different ways, because people creating new templates, cloning, copy pasting, etc. could all screw that up. Jan 16, 2018 at 19:07
  • Error:(1, 1) INVALID_FIELD_FOR_INSERT_UPDATE - SObject type does not allow triggers: EmailTemplate Jan 24, 2018 at 13:20

1 Answer 1


In response to ataneja's question, here is how we did it:

  • We added the template id at the end of all text email templates, using a script. This means ids like "00X0J000002L0s5UAC" below the footer of each email template.
  • Add a text field "TemplateId__c" to Email Message.
  • In a beforeInsert trigger on Email Message, we search for the template id in the email text (all template ids start with "00X"). If we find it, we store it on EmailMessage.TemplateId__c.

You must log in to answer this question.

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