I have a classic email template which includes a {!TODAY() + x}
function. I want to dynamically steer this x
via APEX.
Therefore I use .replace()
function in APEX which will look like following:
{!TODAY() + xPLUSDAYS}
in Email Template and emailHTMLBody = emailHTMLBody.replace('xPLUSDAYS', days);
in APEX where days
are dynamically declared, e.g.: String days = '14';
Unfortunately when I try it out, this will lead to a blank area where the date should be. I already tried it directly with e.g. {!TODAY() + 14}
and it worked totally fine (showed e.g. 03/24/2020, when today is 03/10/2020).
I went ahead and changed classic email template to {xTODAYPLUSDAYS}
, APEX to emailHTMLBody = emailHTMLBody.replace('{xTODAYPLUSDAYS}', days);
and declare days like String days = '{!TODAY() +14}';
so that the whole TODAY() function string is changed in a whole.
Unfortunately this will lead to the email actually showing {!TODAY() +14} as Text instead of e.g. 03/24/2020.
I also tried variations of the replace string, with !
or without, with {}
or without, but the basic result always remains one of the two mentioned above (nothing shown or faulty code string shown).
I render the email as follows: Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(templateId, u.Id, u.Id);
Then I go ahead and alter the html body as explained:
String htmlBody = email.getHtmlBody();
htmlBody = htmlBody.replace('{xTODAYPLUSDAYS}', days);
email.setHtmlBody(htmlBody);
Finally I go ahead and send that thing:
Messaging.sendEmail(email, false);
(I actually do all these things within loops for multiple recipients with an allEmail List and a user List, but that shouldn't make a difference)
I already had a look at those "replace merge fields" articles, but I figure this particular issue is quite specific in regard of altering a function in html.
Is there any way to alter a function within the HTML to make e.g. this today+x date flexible? Thanks.
Best Answer
I am new to Salesforce but maybe you need add:
This way SF render your template. In my case I have a similar problem and this solved, hope help somebody, bye (sorry my english)