Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active April 18, 2023 20:22
Show Gist options
  • Save douglascayers/247f895a1e4324505c65 to your computer and use it in GitHub Desktop.
Save douglascayers/247f895a1e4324505c65 to your computer and use it in GitHub Desktop.
Example Visualforce Template with iCal Attachment
<messaging:emailTemplate subject="Close Opportunity"
recipientType="User"
relatedToType="Opportunity">
<messaging:htmlEmailBody >
<html>
<body>
Opportunity: <a href="https://test.salesforce.com/{!relatedTo.id}"><apex:outputText value="{!relatedTo.name}"/></a>
<br/>
<!--
Visualforce dateformat trick, put a whitespace character before the date expression and it will format in org's locale, not GMT
https://douglascayers.com/2014/05/27/salesforce-displaying-datetime-field-in-visualforce-in-users-timezone/
-->
Close Date: <apex:outputText value=" {!relatedTo.CloseDate}"/>
<br/>
</body>
</html>
</messaging:htmlEmailBody>
<messaging:attachment filename="reminder.ics">
BEGIN:VCALENDAR
METHOD:PUBLISH
VERSION:2.0
PRODID::-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
ORGANIZER:mailto:{!recipient.Email}
DTSTAMP;TZID=GMT:<apex:outputText value="{0,date,yyyyMMdd'T'HHmmssZ}"><apex:param value="{!relatedTo.CloseDate}"/></apex:outputText>
UID:<apex:outputText value="{!relatedTo.id}"/>
DTSTART;TZID=GMT:<apex:outputText value="{0,date,yyyyMMdd'T'HHmmssZ}"><apex:param value="{!relatedTo.CloseDate}"/></apex:outputText>
DTEND;TZID=GMT:<apex:outputText value="{0,date,yyyyMMdd'T'HHmmssZ}"><apex:param value="{!relatedTo.CloseDate}"/></apex:outputText>
SUMMARY:<apex:outputText value="Close Opportunity {!relatedTo.name}"/>
DESCRIPTION:<apex:outputText value="{!JSENCODE(relatedTo.description)}" escape="false"/>\n\n<apex:outputText value="https://test.salesforce.com/{!relatedTo.id}" escape="false"/>
BEGIN:VALARM
TRIGGER:-PT1D
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
</messaging:attachment>
</messaging:emailTemplate>
@douglascayers
Copy link
Author

This builds upon the vCalendar visualforce page example, https://gist.github.com/DouglasCAyers/ada5a2a1708099f6ed7e, by embedding the iCal tags to arbitrarily create an attachment.

@douglascayers
Copy link
Author

To specify an All Day event then remove the DTEND line and change the DTSTART to be specify VALUE=DATE and format as YYYYMMDD:

DTSTART;VALUE=DATE:<apex:outputText value="{!MID(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(relatedTo.CloseDate),':',''),'-',''),' ','T'),1,8)}"/>

Please note, the TEXT() function is still using the GMT variant of the date/time, so you may need to do extra work to adjust for time zone differences, perhaps a custom visualforce component might have to get involved or a formula field that already takes into account your organization's time zone offset.

Reference: http://tools.ietf.org/html/rfc5546#section-4.1.5

@douglascayers
Copy link
Author

I've updated the DTSTART, DTEND, and DTSTAMP to use <apex:outputText> fomatting option for IS8601 output.

@douglascayers
Copy link
Author

Check out my new blog post for a solution that does not require Visualforce or attachments, https://douglascayers.com/2019/06/22/how-to-send-google-and-outlook-calendar-invite-links-in-email-templates/

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