Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create .ics Calendar Event in Visualforce
Simple proof-of-concept to create a .ics calendar event with visualforce.
Inspired by Natalie Regier @gnatrae
This example uses url parameters to set the details of event.
You could create a custom button that invokes this page, like:
https://<your-salesforce-domain>/apex/vCalendarPage?start={!TEXT(Obj.StartDate__c)}&end={!TEXT(Obj.EndDate__c)}&subject=Event from Visualforce&description=This .ics event created with visualforce&location=The Cloud
An example with literal values you can copy & paste in your browser:
https://<your-salesforce-domain>/apex/vCalendarPage?start=20140524T140000&end=20140524T153000&subject=Event from Visualforce&description=This .ics event created with visualforce&location=The Cloud
<apex:page title="event.ics" contentType="text/calendar" cache="false">
PRODID::-//hacksw/handcal//NONSGML v1.0//EN
DTSTAMP;TZID=GMT:<apex:outputText value="{!SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($CurrentPage.parameters.start,':',''),'-',''),' ','T')}"/>
UID:<apex:outputText value="{!SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($CurrentPage.parameters.start,':',''),'-',''),' ','')}"/>
DTSTART;TZID=GMT:<apex:outputText value="{!SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($CurrentPage.parameters.start,':',''),'-',''),' ','T')}"/>
DTEND;TZID=GMT:<apex:outputText value="{!SUBSTITUTE(SUBSTITUTE(SUBSTITUTE($CurrentPage.parameters.end,':',''),'-',''),' ','T')}"/>
SUMMARY:<apex:outputText value="{!$CurrentPage.parameters.subject}"/>
DESCRIPTION:<apex:outputText value="{!$CurrentPage.parameters.description}"/>
LOCATION:<apex:outputText value="{!$CurrentPage.parameters.location}"/>
Copy link

Although the visualforce code understands the English locale text format of dates when referenced as {!Obj.Date} (e.g., 2014-05-09 15:36:22Z). If you needed other locale support you can format the date/time fields into the expected url parameter format with this formula (replacing CreatedDate with your date/time field):

TEXT( YEAR( DATEVALUE( CreatedDate ) ) )
LPAD( TEXT( MONTH( DATEVALUE( CreatedDate ) ) ), 2, "0" )
LPAD( TEXT( DAY( DATEVALUE( CreatedDate ) ) ), 2, "0" )
& "T" &
MID( TEXT( CreatedDate ), 12, 2 )
MID( TEXT( CreatedDate ), 15, 2 )
MID( TEXT( CreatedDate ), 18, 2 )

Copy link

Copy link

Hi Douglas, great post.
Maybe you could help me a little bit.
I'm sending from an Apex class an Messaging.SingleEmailMessage, and I need to attach an ICS file.
I already have created your page.
I have this piece of code.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

So my question is, how do I attach your page to my email???
Should I set the PageReference on email.setFileAttachments(.........)????

Thank you very much

Copy link

Avelino, I think this is addressed in the link above provided by Douglas.

In the solution section on the referenced link it says:
"Configure the email template like normal (subject, recipient, relatedTo, body) then at the bottom add <messaging:attachment filename=”reminder.ics”> and closing tag </messaging:attachment>. Between the two attachment tags, include the iCal markup to define your event as shown in this gist. "

Copy link

new84js commented Dec 14, 2018

Thanks for this code.

When i am sending this invite to GMAIL, on mobile app, i am unable to see the description in calendar.

you can check, and let me know, if it is working for you.

many thanks,

Copy link

ajhare commented Jun 21, 2019

Thank you for the above!
I am also experiencing the same issue as new84js, in regards to the body of the event(description) is being dropped. I've tried tweaking it and was able to get the body to show the expected values but the formatting was dropped. It displays in html and I can't seem to get passed that.
If you or anyone has any thoughts or tips I'd appreciate it!


Copy link

Check out my new blog post for a solution that does not require Visualforce or attachments,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment