Hello Everyone, In this tutorial, I am going to show a code sample that helps you to send a calendar invite that format support MS Outlook calendar. Before using this sample code, you have to configure MS Outlook with your email-id. So without wasting time let's get started,
Send calendar invites in MS Outlook
Step 1: Configure your email with MS Outlook.
Step 2: Login to your Salesforce Org. and open developer console.
Step 3: Navigate to File | New | Apex Class and Create an Apex controller called MSOutlookCalendarInvites using the following syntax.
MSOutlookCalendarInvites.apxc
public class MSOutlookCalendarInvites { public static void Sendinvite() { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new List<String>{'smanisheng@gmail.com'}); mail.setSubject('Meeting Invitation'); mail.setPlainTextBody(''); Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); attach.filename = 'reminder.ics'; attach.ContentType = 'text/calendar; charset=utf-8; method=REQUEST'; attach.inline = true; attach.body = invite(); mail.setFileAttachments(new Messaging.EmailFileAttachment[] {attach}); Messaging.SendEmailResult[] er = Messaging.sendEmail(new Messaging.Email[] { mail }); } private static Blob invite() { DateTime dt=DateTime.now().adddays(1); String startdatetime = String.valueof(dt.year()+'0'+dt.month()+''+dt.day()+'T110000'); String enddatetime = String.valueof(dt.year()+'0'+dt.month()+''+dt.day()+'T100000'); String txtInvite = ''; txtInvite += 'BEGIN:VCALENDAR\n'; txtInvite += 'PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN\n'; txtInvite += 'VERSION:2.0\n'; txtInvite += 'METHOD:REQUEST\n'; txtInvite += 'CALSCALE:GREGORIAN \n'; txtInvite += 'X-MS-OLK-FORCEINSPECTOROPEN:TRUE\n'; txtInvite += 'BEGIN:VEVENT\n'; txtInvite += 'CLASS:PUBLIC\n'; txtInvite += 'CREATED:20191026T203709Z\n'; txtInvite += 'DTEND:'+enddatetime+'\n'; txtInvite += 'DTSTAMP:20191026T203709Z\n'; txtInvite += 'DTSTART:'+startdatetime+'\n'; txtInvite += 'LAST-MODIFIED:20191026T203709Z\n'; txtInvite += 'LOCATION:US\n'; txtInvite += 'PRIORITY:5\n'; txtInvite += 'SEQUENCE:0\n'; txtInvite += 'ACTION:DISPLAY\n'; txtInvite += 'SUMMARY;'; txtInvite += 'LANGUAGE=en-us:Meeting Reminder\n'; txtInvite += 'TRANSP:OPAQUE\n'; txtInvite += 'UID:'+startdatetime+'Z-4000F192713-0052@salesforce.com\n'; txtInvite += 'X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><META NAME="Generator" CONTENT="MS Exchange Server version 08.00.0681.000"><TITLE></TITLE></HEAD><BODY></BODY></HTML>\n'; txtInvite += 'X-MICROSOFT-CDO-BUSYSTATUS:BUSY\n'; txtInvite += 'X-MICROSOFT-CDO-IMPORTANCE:1\n'; txtInvite += 'END:VEVENT\n'; txtInvite += 'END:VCALENDAR'; return Blob.valueOf(txtInvite); } }
Step 4: In developer console, Navigate to Debug | Open Execute Anonymous Window or press CTRL+E and execute the following line of code to see the output (don't forget to replace setToAddresses with your email in above code example),
MSOutlookCalendarInvites.Sendinvite();Output:
The recipient can accept, tentative or decline meeting invitation using the highlighted button in above image in their own outlook inbox. If the recipient accepts the meeting invitation a calendar entry has created in their MS-outlook calendar as a reminder.
See also:
- Send Email Using Custom Email Template in Salesforce
- Query Deleted Records in Salesforce
- Webservice Keyword in Apex
Conclusion:
Hope you like this tutorial, for any query or suggestions
please feel free to comment.
Thank you.
1 Comments
I tried using this method to create a calendar invite on an apex trigger, to notify employees when their time off requests have been approved and to put a meeting on their calendars so that other employees know they are unavailable. However, the time fields on my trigger are relative to the time I made the trigger. I made the trigger at 5 pm, so if I set the time to "T000000Z", it starts at 5 pm. Is there any way to fix this?
ReplyDeletePost a Comment