1

We have issue in getting the EmailTemplate id from EmailMessage object . After we send email the emailTemplate Id is not populated. This email is sent using a template

List<EmailTemplate> lstEmailTemplates = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'XXXXX'];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(lstEmailTemplates[0].Id);
mail.setSaveAsActivity(true);
//set addresses
mail.setToAddresses(toAddresses);
mail.setTargetObjectId(lsAcr[0].ContactId); // Any contact Id of your record
mail.setTreatTargetObjectAsRecipient(false);
mail.setUseSignature(false);
mail.setWhatId(caseId);

The field EmailTemplateId was added so that we can track which template was used for the email in Spring 19 release.

When we query EmailMessage object using the Id of the email sent we see that EmailMessageId is blank ?

Is our understanding incorrect on the usage of this field ?

4
  • How are you putting in the values in "lstEmailTemplates"? I just checked on workbench in my org and emails after Spring 19 release have a value in EmailTemplateId Commented Nov 20, 2019 at 13:11
  • @KrisGoncalves i have updated the snippet. Have you used the template in Case.Email feed or sent email through SingleEmailMessage api. Commented Nov 20, 2019 at 13:25
  • I might have misunderstood. You're saying your code works fine and sends an email, but when you check the email message afterwards you see no value in EmailTemplateId even though you set it? Commented Nov 20, 2019 at 13:34
  • Yes, thats correct! Commented Nov 20, 2019 at 13:59

1 Answer 1

1

It might still be worth creating a case with Salesforce to really confirm the behavior we are seeing. Otherwise, it seems it only works within the Lightning Experience which means emails sent from apex don't get stamped. I tested in my org as well as follows:

  1. Using apex to send an email with a email template = did not set the EmailTemplateId field on the EmailMessage record.

  2. Using the email action within Lightning experience and using an email template = did set the EmailTemplateId on the EmailMessage record

Looking closer at the documentation, it's interesting to note the description where it mentions the field is only populated for Lightning Experience which seems to suggest some UI-based need for it to work versus it working in apex execution.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm

enter image description here

1
  • Thank you Kris. This same we noticed and altered our approach on not to reply on EmailtemplateID for performing some logic. Commented Nov 22, 2019 at 12:35

You must log in to answer this question.

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