Is there a way to retrieve the merge fields of a specific Lightning Email Template in Apex?


EmailTemplate template = new EmailTemplate();

I would like to validate the merge fields so that an email is not sent out with empty fields, as is the current behavior.

The EmailTemplate docs do not seem to discuss this.

  • 1
    Might not be what you want but have you looked at Error while using blank fields while sending email via lightning Activity tab – codeyinthecloud Dec 28 '18 at 18:49
  • May be you need to look into this documentation developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Sudipta Deb Dec 28 '18 at 18:53
  • @codeyinthecloud I want to be able to throw an apex exception if the merge field is blank. I also would rather not use a separate Formula Field. Those keep adding up and make the object have a lot of extra fields. Thank you. – shmuels Dec 28 '18 at 18:53
  • @SudiptaDeb This may be exactly what I'm looking for. I will look into it and see if it works. Thank you. – shmuels Dec 28 '18 at 18:55
  • 1
    @shmuels I'm not so sure, that service looks like it pulls in all fields for a given object whether or not it appears in the template. What you could do is pull in the text of the template body and parse the merge fields yourself instead of relying on SF. that way if you are missing data you can stop the email. – gNerb Dec 28 '18 at 19:50

Use this link to find out the EmailTemplateMergeFields. It will give you all the merge fields possible for an object. At the same time parse text of your emailTemplate body. With two lists created, you can write your business validation. Basically with two lists in your hand, you have enough power to play around through Apex.

  • What would be the benefit of the first list, once I need to already parsing the Template body? – shmuels Dec 30 '18 at 1:34
  • That depends on your business logic. I just thought of sharing some options to you which you can use to implement your business logic. – Sudipta Deb Dec 30 '18 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.