1

We are creating a Visualforce email template to be sent when certain Events are saved/edited to meet criteria. The VF Template is related To Event/Contact:

<messaging:emailTemplate subject="Appointment Reminder" recipientType="Contact" relatedToType="Event">

I am working on outputting the Date, and Start - End time of the Appointment (Event), in EST, to send to our customers, like this:

Event Name

05/08/2017

05:00 PM - 6:00 PM EST

Currently I am using the following formatting to output the date and date/time values to the template:

<apex:outputText value="{0,date,MM/dd/yyyy}">
    <apex:param value="{!relatedTo.ActivityDate}" />
</apex:outputText><br />

<apex:outputText value="{0,date,hh:mm:ss a}">
    <apex:param value="{!relatedTo.StartDateTime}" />
</apex:outputText>
 - 
<apex:outputText value="{0,date,hh:mm:ss a}">
    <apex:param value="{!relatedTo.EndDateTime}" />
</apex:outputText><br />

However, most of us probably already know that Salesforce stores datetime values in GMT. My appointment is booked at 5-6PM EST, but the GMT conversion makes it 9-10PM GMT, so the above code is resulting in this output:

Template Example 1


I would like to output the times in Eastern, regardless of who the template is being sent to, since all of our customers know that we book in eastern time. I could possibly hard code this with something like {!relatedTo.StartDateTime-(4/24)} but that will only work until we change times during Daylight savings spring forward/fall back times.

Question: I'm trying to avoid making a component and a custom controller for the purposes of outputting just 2 date fields, so figured I'd come to the best Salesforce community I know to see if there were any trade secrets I could use or things I missed that would allow me to force the output into EST - specifically in a way that accounts for daylight savings and doesn't require a component and controller. Is this possible?

1 Answer 1

1

You need to use custom controller or extension to build the logic of offset.

You can easily get the timezone string of the user by:

UserInfo.getTimeZone();

Then add the offset to your datetime:

d.addHours(the offest);

Use this method

public static Integer timeZoneOffset_hours(Date d, String tz) {
    TimeZone tzSelected = TimeZone.getTimeZone(tz);
    TimeZone tzGmt = TimeZone.getTimeZone('Greenwich Mean Time');

    Integer gmtOffset = tzGmt.getOffset(d);
    Integer selectedOffset = tzSelected.getOffset(d);
    Integer hours = (selectedOffset - gmtOffset) / 1000 / 60 / 60; 

    return hours;
}

And finally display that on screen.

2
  • We can't use custom controllers directly in Visualforce email templates, which means in order to implement your solution I would need a VF component with an apex controller to embed in the VF email template. May 10, 2017 at 15:01
  • My logic is different than yours - I ended up just passing the GMT datetime that Salesforce already stores into the DateTime.format() method and passed in the desired TimeZone for my formatted response dt.format(gmtDate,'America/New_York') May 10, 2017 at 15:02

You must log in to answer this question.

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