You need to sign in to do that
Don't have an account?
SeanCo
[Visualforce Email Template] Current Month in Subject?
Hi,
I am trying to see if it is possible to dynamically insert the current month into the 'subject' line of a VisualForce email template. More specifically, I need to display the month in 'MMMMM' format (Ex: December). I am aware that the following expressions work:
- {!TODAY()}
- {!MONTH(TODAY())}
- {!DAY(TODAY())}
- {!YEAR(TODAY())}
Example:
<messaging:emailTemplate subject="{!MONTH(TODAY())} This is my subject!" recipientType="Contact" relatedToType="Custom_Object__c" replyTo="everyone@world.com" >
Any way to format the date in 'MMMMM' format (Ex: December)?
Thanks in advance!
I'm surprised nobody noticed this... Visualforce is an XML-based language. As such, you can't have literal quotes inside of an attribute's value. Use apostrophes ("single quote") instead:
Note that the detail page will show the subject as the current month (the formula is executed even in the template detail page!).
Edit: I have noticed that you can "sometimes" get away with using quotation marks, but for consistency, I recommend only apostrophes when placing formulas inside an attribute.
All Answers
I don't think there's any option besides CASE():
Hi thomast,
Thanks for the reply. I tried out your suggestion, but received the following error:
Here is a copy of my code:
Can you see anything obviously wrong? Thanks again for your help!
MONTH(TODAY()) returns a number, not a string. Take the quotes off of all the numbers in your CASE statement.
I added the quotes after it was originally generating the error message stated above. Obviously, adding them did not help matters ;)
I have tried the following:
And even a more simplistic version:
...and received the same error message as earlier. The error is definitely related to this statement as once it is removed it saves/deploys without any issues.
MONTH is missing its closing parens. You need two closing parens after TODAY:
subject="{!CASE(MONTH(TODAY()),1,"January",2,"February")}"
I think I missed that up when I was pasting the statement. I made the adjustment, but still getting the error unfortunately. Screenshoot below:
Right -- so the parser is getting past the opening tag, which is good.
Your <messaging:emailTemplate> should have either or both of a <messaging:plainTextEmailBody> </messaging:plainTextEmailBody>
<messaging:htmlTextEmailBody> </messaging:htmlEmailBody>
and then a closing </messaging:emailTemplate> tag. Does it?
It appears to have all the proper tags....here's a copy of the template (minus the main body content):
I'm surprised nobody noticed this... Visualforce is an XML-based language. As such, you can't have literal quotes inside of an attribute's value. Use apostrophes ("single quote") instead:
Note that the detail page will show the subject as the current month (the formula is executed even in the template detail page!).
Edit: I have noticed that you can "sometimes" get away with using quotation marks, but for consistency, I recommend only apostrophes when placing formulas inside an attribute.
@thomast: Thanks for pointing me in the right direction and all the help along the way!
If you do not mind I do have one additional question (slightly off topic)...in the formula why does the month of 'December' not have a corresponding numerical value of 12? And why does adding a value cause an error?
The last option in the CASE() statement is the "ele", the value returned if none of the other conditions are met. If you add the 12, then there's no "else", which is required. You could finish it off like this:
CASE(MONTH(TODAY()),
....
11, 'November',
12,'December',
'Apocalypse')
@Thomas Taylor: Thanks for the clarification...makes perfect sense.