function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SeanCoSeanCo 

[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!

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

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:

 

 

<messaging:emailTemplate subject="{!case(month(today()),1,'January',2,'February',3,'March',4,'April',5,'May',6,'June',7,'July',8,'August',9,'September',10,'October',11,'November','December')}" recipientType="Contact" >
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

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

thomastthomast

I don't think there's any option besides CASE():

 

<messaging:emailTemplate subject="{!CASE(MONTH(TODAY()),
1,"January",
2,"February",
3,"March",
4,"April",
5,"May",
6,"June",
7,"July",
8,"August",
9,"September",
10,"October",
11,"November",
"December")} This is my subject!" 
SeanCoSeanCo

Hi thomast,

 

Thanks for the reply.  I tried out your suggestion, but received the following error:

 

 

Save error: Element type "messaging:emailTemplate" must be followed by either attribute specifications, ">" or "/>".	

 

 

Here is a copy of my code:

 

 

<messaging:emailTemplate subject="{!CASE(MONTH(TODAY(), 
"1","January", 
"2","February", 
"3","March", 
"4","April", 
"5","May", 
"6","June", 
"7","July", 
"8","August", 
"9","September", 
"10","October", 
"11","November", 
"12","December")
} This is my subject!" 
recipientType="Contact"
relatedToType="Custom_Object__c"
replyTo="everyone@world.com" >

 

 

Can you see anything obviously wrong?  Thanks again for your help!

 

 

thomastthomast

MONTH(TODAY()) returns a number, not a string. Take the quotes off of all the numbers in your CASE statement.

 

SeanCoSeanCo

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:

 

 

subject="{!CASE(MONTH(TODAY(),1,"January",2,"February",3,"March",4,"April",5,"May",6,"June",7,"July",8,"August",9,"September",10,"October",11,"November",12,"December")}"

 

 

And even a more simplistic version:

 

 

subject="{!CASE(MONTH(TODAY(),1,"January",2,"February")}"

 

...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.

 

thomastthomast

MONTH is missing its closing parens. You need two closing parens after TODAY: 

 

subject="{!CASE(MONTH(TODAY()),1,"January",2,"February")}"

SeanCoSeanCo

I think I missed that up when I was pasting the statement.  I made the adjustment, but still getting the error unfortunately.  Screenshoot below: 

 

thomastthomast

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?

SeanCoSeanCo

It appears to have all the proper tags....here's a copy of the template (minus the main body content): 

 

 

 

<messaging:emailTemplate subject="{!CASE(MONTH(TODAY()),1,"January",2,"February")}"
recipientType="Contact"
relatedToType="Custom_Object__c"
replyTo="everyone@world.com" >

<messaging:htmlEmailBody >        

<html>
<head>
	<title>Template Title</title>
</head>
 
<body>

	<p>Main HTML template body goes here.</p>
 
</body>
</html>        

</messaging:htmlEmailBody> 

<messaging:plainTextEmailBody >

	<p>Main TEXT template body goes here.</p>
	
</messaging:plainTextEmailBody>    

</messaging:emailTemplate>

 

 

sfdcfoxsfdcfox

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:

 

 

<messaging:emailTemplate subject="{!case(month(today()),1,'January',2,'February',3,'March',4,'April',5,'May',6,'June',7,'July',8,'August',9,'September',10,'October',11,'November','December')}" recipientType="Contact" >
<messaging:plainTextEmailBody >
Congratulations!
This is your new Visualforce Email Template.
</messaging:plainTextEmailBody>
</messaging:emailTemplate>

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.

This was selected as the best answer
SeanCoSeanCo

 

@sfdcfox: Thanks...switching the quotation marks to apostrophes resolved the error!  The month is displaying as intended now.

@thomast: Thanks for pointing me in the right direction and all the help along the way!

 

SeanCoSeanCo

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?

Thomas TaylorThomas Taylor

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')

SeanCoSeanCo

@Thomas Taylor:  Thanks for the clarification...makes perfect sense.