+ Start a Discussion
Christian Szandor KnappChristian Szandor Knapp 

How to produce correct DateTime Format in Apex: Outputlink / Apex: Param

I am using a visualforce page email template in which Recipient is a Contact, relatedTo is an Event, it dynamically generates an *.ical Attachment. That works just fine by using

<apex:outputText value="{0,date,yyyyMMdd'T'HHmmss}"><apex:param value="{!relatedTo.StartDateTime}" /></apex:outputText>

What I am not able to achieve is to render a link to Google Calendar that needs the same formatting of StartDateTime/EndDateTime.

I've tried using outputtext as well, but SF prevents me of using that in < a href=''>, if I dont use < a href=''>, some clients wont render the Link as Link, i.e. it is not clickable which is not acceptable. For other reasons, I need to used < messaging:HtmlEmailBody > and cannot use PlainText.

So I tried dissecting my link into params and use Apex:Outputlink.

My approach is as follows:

<apex:OutputLink value="https://www.google.com/calendar/render">
Click me
<apex:param name="action" value="TEMPLATE"/>
<apex:param name="text" value="someValue"/>
<apex:param name="dates" value="{relatedTo.Startdatetime}Z/{!relatedTo.EndDateTime)}Z"/>
<apex:param name="details" value="someOtherValue"/>
<apex:param name="sf" value="true"/>
<apex:param name="output" value="xml"/>
</apex:OutputLink>



Is there any way to get that relatedTo.Startdatetime into the format yyyyMMdd followed by HHmmss in an apex param?

If not, can you think of any workarounds that work with the event object? (FYI: SF does not allow to use StartDateTime in a formula expression to create costums fields of the format needed)
Shikha AgashiShikha Agashi
Did you find the solution for above issue?
Ch. Szandor KnappCh. Szandor Knapp
Hello,

I ended up using an apex component in the VF Email Template: 
 
<!-- Component -->
<apex:component controller="compEventGoogleCalendarLinkCtrl" access="global">
    <apex:attribute type="Id" name="CurrentEventId" assignTo="{!idEventId}" description="Passes the Event"/>
    <apex:attribute type="String" name="GoogleCalendarSubject" assignTo="{!strGoogleCalendarSubject}" description="Subject of the Calendar Event"/>
    <apex:attribute type="String" name="LinkToCalendarName" assignTo="{!strLinkToCalendarName}" description="Name of the Calendar Link"/>
 <a style="color: #47a3d1;text-decoration:none;" href="https://www.google.com/calendar/render?action=TEMPLATE&text={!strGoogleCalendarSubject}&dates={!strStartDateTime}/{!strEndDateTime}&details={!strGoogleCalendarSubject}">{!strLinkToCalendarName} {!strStartDateTimeFormatted}</a>
</apex:component>
 
/**
	* compEventGoogleCalendarLinkCtrl - <Used to create a Google Calendar Link in VF Emails>
	* @author: Christian Szandor Knapp
	* @version: 1.0
*/

public class compEventGoogleCalendarLinkCtrl{

public Id idEventId {get; set;}
public String strGoogleCalendarSubject {get; set;}
public String strLinkToCalendarName {get; set;}


	public compEventGoogleCalendarLinkCtrl(){}

public Event getEvent(){
	Event Event = new Event(Subject = 'Test', StartDateTime = System.now(), EndDateTime = System.now()+2);
	if (idEventId != null){
		Event = [SELECT Id WhatId, StartDateTime, EndDateTime, Description, Subject FROM Event WHERE Id =: idEventId ];
    }
	return Event;
}

    public String getstrStartDateTime(){
        String strStartDateTime = getEvent().StartDateTime.format('yyyyMMdd\'T\'hhmmss\'Z\'', 'GMT');
        return strStartDateTime;
    }

	public String getstrEndDateTime(){
        String strEndDateTime = getEvent().EndDateTime.format('yyyyMMdd\'T\'hhmmss\'Z\'', 'GMT');
        return strEndDateTime;
    }

	public String getstrStartDateTimeFormatted(){
        String strStartDateTimeFormatted = getEvent().StartDateTime.format('dd.MM.yyyy\' - \'HH:mm');
        return strStartDateTimeFormatted;
    }

	public String getstrLinkToCalendarName(){
		if (strLinkToCalendarName.length()>0)
		return strLinkToCalendarName;
		else
		return 'Click me';
	}

	public String getstrGoogleCalendarSubject(){
		if (strGoogleCalendarSubject.length()>0)
		return strGoogleCalendarSubject;
		else
		return 'Your Setup';
	}

}



We ditched that since we couldn't get TimeZones to work properly in the end. Also, Gmail started to better handle ics attachments, so our usecase was weakened. 

Kind regards,
Sz