If you’ve ever wondered how to send an outlook calendar invite from the salesforce apex, here’s everything you need to know.
Meeting invite
Let's code
Step 1: Create a new class to generate meeting invite (class name: NPS_MeetingInviteGenerator)
//codekiat.com
public class
NPS_MeetingInviteGenerator {
public String startDate{get;set;}
public String endDate{get;set;}
public String meetingSubject{get;set;}
public String meetingBody{get;set;}
public String displayName{get;set;}
public String attachmentName{get;set;}
public string fromAddress{get;set;}
public string toAddressList{get;set;}
public Messaging.EmailFileAttachment
generateMeetingInvite(){
Messaging.EmailFileAttachment
meetingInviteIcs = new Messaging.EmailFileAttachment();
Blob b = doIcsAttachment();
meetingInviteIcs.setFileName(attachmentName);
meetingInviteIcs.setBody(b);
return meetingInviteIcs;
}
public Blob doIcsAttachment(){
String [] icsTemplate = new
List<String> {'BEGIN:VCALENDAR',
'PRODID:-//Schedule a Meeting',
'VERSION:2.0',
'METHOD:REQUEST',
'BEGIN:VEVENT',
'DTSTART: ' + startDate,
'DTEND: ' + endDate,
'UID: ' +
String.valueOf(Crypto.getRandomLong()),
'DESCRIPTION: ' + meetingBody,
'X-ALT-DESC;FMTTYPE=text/html: ' +
meetingBody,
'SUMMARY: ' + meetingSubject,
'ORGANIZER:MAILTO: ' + fromAddress,
'ATTENDEE;CN="' + displayName
+ '";RSVP=TRUE:mailto: ' + toAddressList,
'BEGIN:VALARM',
'TRIGGER:-PT1080M',
'ACTION:DISPLAY',
'DESCRIPTION:Reminder',
'END:VALARM',
'END:VEVENT',
'END:VCALENDAR'
};
return
Blob.valueof(String.join(icsTemplate, '\n'));
}
}
//codekiat.com
public class
NPS_EmailUtility {
public String fromAddress{get;set;}
public String[] toAddressList{get;set;}
public String subject{get;set;}
public String body{get;set;}
public Id orgWideEmailAddrId{get;set;}
public
List<Messaging.EmailFileAttachment> emailAttachmentList{get;set;}
public void sendEmail(){
Messaging.SingleEmailMessage emailMsg =
new Messaging.SingleEmailMessage();
List<Messaging.Emailfileattachment> fileAttachments = new
List<Messaging.Emailfileattachment>();
emailMsg.setReplyTo(fromAddress);
emailMsg.setToAddresses(toAddressList);
emailMsg.setSubject(subject);
emailMsg.setHtmlBody(body);
emailMsg.setOrgWideEmailAddressId(orgWideEmailAddrId);
if(emailAttachmentList != null
&& emailAttachmentList.size() > 0){
for(Messaging.Emailfileattachment
efa: emailAttachmentList){
fileAttachments.add(efa);
}
}
emailMsg.setFileAttachments(fileAttachments);
system.debug('Send email >> ' +
emailMsg);
try{
Messaging.sendEmail(new
Messaging.SingleEmailMessage[] { emailMsg });
}catch(System.EmailException ex){
system.debug('Issue in sending
email >> ' + ex.getMessage());
}
}
}
Test this functionality in the Developer
console > Anonymous window
//Generate Meeting
invite
NPS_MeetingInviteGenerator
icsGenerator = new NPS_MeetingInviteGenerator();
List<Messaging.Emailfileattachment>
fileAttachments = new List<Messaging.Emailfileattachment>();
icsGenerator.meetingSubject
= 'Invite - Test subject';
icsGenerator.meetingBody
= 'Invite - Test body';
icsGenerator.startDate
= '04/10/2021 10:30:00PM';
icsGenerator.endDate
= '14/10/2021 10:30:00PM';
icsGenerator.fromAddress
= 'test@example.com';
icsGenerator.displayName
= 'NPS';
icsGenerator.attachmentName
= 'MeetingInvite.ics';
Messaging.EmailFileAttachment
meetingInviteIcs = icsGenerator.generateMeetingInvite();
fileAttachments.add(meetingInviteIcs);
NPS_EmailUtility
emailUtils = new NPS_EmailUtility();
emailUtils.toAddressList
= new String[] {'test@example.com'};
emailUtils.subject =
'Test email subject';
OrgWideEmailAddress[]
orgWideEmailAddr = [select Id from OrgWideEmailAddress];
if
(!orgWideEmailAddr.isEmpty()) {
emailUtils.orgWideEmailAddrId =
orgWideEmailAddr[0].Id;
}
emailUtils.body =
'Test attachment body';
emailUtils.emailAttachmentList
= fileAttachments;
emailUtils.sendEmail();
Execute the code and check your outlook mailbox.
Considerations
As the meeting invite includes the dates
logic, if your application is implemented for multiple time zones, make
sure to convert the time to the respective time zone.
Code for converting the date to user time
zone
public static string
convertDateTimeToUserTimezone(DateTime DateInGMT, String TimeZoneKey){
return DateInGMT.format('yyyy-MM-dd\'T\'HH:mm:ss', TimeZoneKey);
}
Conclusion
Now you know how to send an email with an attachment in salesforce lightning.
Let me know your thoughts and feedback in the
comments section below and also, I would like to know if you have tried any other
approach to send a meeting invite to the outlook.
In the upcoming post, we will discuss inserting the user data using an apex data loader with important factors to
consider.
Until then…
1 Comments
Great post! But the Start Date and End Date times are not picking up correctly. Whatever datetime value we give, its creating an invite to next 60 minutes.
ReplyDelete