3

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

Example

EmailTemplate template = new EmailTemplate();
template.getMergeFields();

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.

6
  • 1
    Might not be what you want but have you looked at Error while using blank fields while sending email via lightning Activity tab Commented Dec 28, 2018 at 18:49
  • May be you need to look into this documentation developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… Commented Dec 28, 2018 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
    Commented Dec 28, 2018 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
    Commented Dec 28, 2018 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
    Commented Dec 28, 2018 at 19:50

1 Answer 1

1

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.

2
  • What would be the benefit of the first list, once I need to already parsing the Template body?
    – shmuels
    Commented Dec 30, 2018 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. Commented Dec 30, 2018 at 2:03

You must log in to answer this question.

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