You need to sign in to do that
Don't have an account?
Visualforce Email template to display date and time in User's timezone (local time)
Hello All,
Just want to share my experience in creating Visualforce Email template where you want to display date and time in User timezone
***Overview
You are in MST time zone.
Let's say we have Date/Time field TaskDateTime__c and use it in Visualforce email template to show recipient the time in email.
<apex:outputText value="{0,date,EEEE, MMMM d, yyyy, HH:mm:ss}"><apex:param value="{!RelatedTo.TaskDateTime__c}" /></apex:outputText>
In email time will be converted and showed to GMT instead of EST.
The outputField (which convert time to user locale automatically) does not
work in Visualforce Email templates (not supported).
So how do I tell to display in Visualforce Email template time in User
timezone? For example MST?
***Implementation Details (worarounds)***
I found two workarounds:
Workaround 1: Use Controller class for the Component to use in template
In order to format the date in various TimeZone in Email Template, we can follow the steps:
1. First create a Controller class for the Component
Setup|App Setup|Develop|Apex Classes|New
Formatted_DateTime_Controller Class Code
----------------------------------------------------------
public class controller_formatted_datetime { public DateTime date_time { get; set; } //property that reads the datetime value from component attribute tag public String defined_format { get; set;} //property that reads the string value from component attribute tag public String getFormattedDatetime() { if (date_time == null) {return ''; } else { if (defined_format == null) { return date_time.format(); //return the full date/time in user's locale and time zone } else { return date_time.format(defined_format,'MST'); //Specify Time zone like IST,CST }}}}
2. Create a Component
Setup|App Setup|Develop|Components
VF Component Code (Name: VFEmailTempComp)
<apex:component access="global" controller="controller_formatted_datetime">{!FormattedDatetime} <apex:attribute assignTo="{!date_time}" description="The DateTime value to be rendered" name="date_time_value" type="DateTime"></apex:attribute> <apex:attribute assignTo="{!defined_format}" description="The optional format to use for the DateTime value to be rendered" name="date_time_format" type="String"></apex:attribute> </apex:component>
3. Create an Email Template and embed the component.
Setup|Administration Setup|Communication Templates|Email Templates
Email Template Code
---------------------------------
<messaging:emailTemplate subject="Testing DateTime Format" recipientType="Contact" > <messaging:plainTextEmailBody > Formatted: <c:VFEmailTempComp date_time_value="{!NOW()}" date_time_format="EEE MMM d kk:mm:ss z yyyy" /> </messaging:plainTextEmailBody> </messaging:emailTemplate>
It will display current time in MST timezone.
Workaround 2: Calculate difference in the apex:param value (for MST (-7) it will be 7/24=0.291666666
Date: <apex:outputText value="{0,date,EEEE, MMMM d, yyyy}"><apex:param value="{!RelatedTo.Scheduled_Clock_In__c - 0.291666666}" /></apex:outputText> Time: <apex:outputText value="{0,date,h:ma}"><apex:param value="{!RelatedTo.Scheduled_Clock_In__c - 0.291666666}" /></apex:outputText> MST
Thanks Solo. The method 2 was what I was looking for. It works awesome!
I want see the output of vf Tempalte..Can you please attach the output screen short..
Thanks,
Allways Cool
Hello,
See attached screens below - hope this will help you.
Regards,
Evgeny
P.S. Please "Like this" if you will find my advice helpful.
Thanks for the workarounds. I really like Workaround 1. I have followed your steps 1 & 2, but stuck on 3. Stupid question most likely but how do I apply this to my already existing template for example:
<apex:outputText
value=
"{0,date,EEEE, MMMM d, yyyy, HH:mm:ss}"
>
<apex:param
value=
"{!RelatedTo.TaskDateTime__c}"
/
>
</apex:outputText
>
Mnay thanks for any input
Sonya
If you are done with Step 1 & 2 you will be able to use something like this instead of your <apex:outputText ...
<c:VFEmailTempComp date_time_value="{!RelatedTo.TaskDateTime__c}" date_time_format="EEEE, MMMM d, yyyy, HH:mm:ss" />
Hope this will help.
Regards,
Evgeny
Thanks a lot for this. This worked. However I have another slight problem. I am based in London and current we are in British Summer Time (GMT +1.00). However when I type in BST onto the class defined format, it seems to be calculating Bangladesh Standard Time and not British Summer Time. How would I achieve this do you know? Would it be GMT (+1.00) ?
Again grateful for any help. Apex is very new to me.
Thanks
Sonya
Actually I have just worked this out - GMT + 1
Thanks for all your help - great post
I followed the steps above and have all of this working in Sandbox as expected. I have now moved on to get this deployed into Production but when doing so I am getting errors related to unit tests. I have been reading up on this and I am guessing I will need to create a unit test for this new class before I can upload to production? Sorry for the basic questions but I am very new to this!
Many thanks,
Sonya
Here is simple test coverage class code which is mandatory to deploy the controller_formatted_datetime class to PROD
Good luck!
Evgeny
@isTest
public class Testcontroller_formatted_datetime {
static testMethod void test() {
controller_formatted_datetime cond = new controller_formatted_datetime();
cond.date_time = null;
String defined_format = cond.defined_format;
String getFormattedDatetime = cond.getFormattedDatetime();
controller_formatted_datetime cond2 = new controller_formatted_datetime();
cond2.date_time = date.newInstance(2012, 9, 22);
String defined_format2 = cond2.defined_format;
String getFormattedDatetime2 = cond2.getFormattedDatetime();
}
}
Sonya
Jothi
Welcome @Jothi!
Will appresiate if you would "Like" my post!
Also visit my blog http://salesforce.websolo.ca (http://salesforce.websolo.ca" target="_blank) for more SFDC tips and tricks
in formula: "{!RelatedTo.Scheduled_Clock_In__c +0.5}"