function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlexRitsonAlexRitson 

I should probably have posted here... - Apex and ICS meeting requests

Hello,

 

I'm a new user with a devil of a problem - I need to create a bit of code that wil allow me to send meeting requests to non-Salesforce users that will put my Salesforce meetings in their diaries, Outlook or other.

 

I put full details of my request on the General board, thinking it could be done from within the custom HTML email templates.  I now realise it's more of an Apex issue.  

 

Would you be so kind as to take a look?  The thread is http://boards.developerforce.com/t5/General-Development/sending-an-ICS-Outlook-compatible-calendar-request-with-workflow/td-p/658603.

 

Best regards,

 

Alex.

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs

Alex - Finally been able to get something working for you.. Of course need further belts and braces but let me know how this goes....

public class SendEmailwithICSAttachment {
    public static void Sendmail_With_IcsAttachment(DateTime startDate, DateTime endDate, String location, String body, String subject, 
                                                                                            String fromAddress, String displayName, String toAddress) {
        String attachmentName = 'Meeting.ics';
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        //Now we have to set the value to Mail message properties
 
        //Note Please change it to correct mail-id to use this in your application
        msg.setReplyTo(fromAddress);
        msg.setSenderDisplayName(displayName);
        msg.setToAddresses(new String[] {toAddress});
        //msg.setCcAddresses(new String[] {'user@acme.com'});// it is optional, only if required
        msg.setSubject(subject);
        msg.setPlainTextBody(body);

        // Create the attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(attachmentName );
        Blob b = doIcsAttachment(startDate, endDate, location, body, subject, fromAddress, displayName, toAddress);
        efa.setBody(b);
        msg.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        // Send the email you have created.
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
    }
    
    private static Blob doIcsAttachment(DateTime startDate, DateTime endDate, String location, String body, 
                                                                        String subject, String fromAddress, String displayName, String toAddress) {
       
        // Now Contruct the ICS file
        String [] icsTemplate = new List<String> {'BEGIN:VCALENDAR',
                                                                                'PRODID:-//Schedule a Meeting',
                                                                                'VERSION:2.0',
                                                                                'METHOD:REQUEST',
                                                                                'BEGIN:VEVENT',
                                                                                'DTSTART: ' + String.valueof(startDate),
                                                                                'DTSTAMP: '+ String.valueof(DateTime.Now()),
                                                                                'DTEND: ' + String.valueof(endDate),
                                                                                'LOCATION: ' + location,
                                                                                'UID: ' + String.valueOf(Crypto.getRandomLong()),
                                                                                'DESCRIPTION: ' + body,
                                                                                'X-ALT-DESC;FMTTYPE=text/html: ' + body,
                                                                                'SUMMARY: ' + subject,
                                                                                'ORGANIZER:MAILTO: ' + fromAddress,
                                                                                'ATTENDEE;CN="' + displayName + '";RSVP=TRUE:mailto: ' + toAddress,
                                                                                'BEGIN:VALARM',
                                                                                'TRIGGER:-PT15M',
                                                                                'ACTION:DISPLAY',
                                                                                'DESCRIPTION:Reminder',
                                                                                'END:VALARM',
                                                                                'END:VEVENT',
                                                                                'END:VCALENDAR'
                                                                                };
        String attachment = String.join(icsTemplate, '\n');
        return Blob.valueof(attachment ); 
    }
}

Just using plain text content here but can easily expand to use HTML in email and attachment.

Do mark this as a solution so that it helps other looking around.

All Answers

vbsvbs

Alex - Finally been able to get something working for you.. Of course need further belts and braces but let me know how this goes....

public class SendEmailwithICSAttachment {
    public static void Sendmail_With_IcsAttachment(DateTime startDate, DateTime endDate, String location, String body, String subject, 
                                                                                            String fromAddress, String displayName, String toAddress) {
        String attachmentName = 'Meeting.ics';
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        //Now we have to set the value to Mail message properties
 
        //Note Please change it to correct mail-id to use this in your application
        msg.setReplyTo(fromAddress);
        msg.setSenderDisplayName(displayName);
        msg.setToAddresses(new String[] {toAddress});
        //msg.setCcAddresses(new String[] {'user@acme.com'});// it is optional, only if required
        msg.setSubject(subject);
        msg.setPlainTextBody(body);

        // Create the attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName(attachmentName );
        Blob b = doIcsAttachment(startDate, endDate, location, body, subject, fromAddress, displayName, toAddress);
        efa.setBody(b);
        msg.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        // Send the email you have created.
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
    }
    
    private static Blob doIcsAttachment(DateTime startDate, DateTime endDate, String location, String body, 
                                                                        String subject, String fromAddress, String displayName, String toAddress) {
       
        // Now Contruct the ICS file
        String [] icsTemplate = new List<String> {'BEGIN:VCALENDAR',
                                                                                'PRODID:-//Schedule a Meeting',
                                                                                'VERSION:2.0',
                                                                                'METHOD:REQUEST',
                                                                                'BEGIN:VEVENT',
                                                                                'DTSTART: ' + String.valueof(startDate),
                                                                                'DTSTAMP: '+ String.valueof(DateTime.Now()),
                                                                                'DTEND: ' + String.valueof(endDate),
                                                                                'LOCATION: ' + location,
                                                                                'UID: ' + String.valueOf(Crypto.getRandomLong()),
                                                                                'DESCRIPTION: ' + body,
                                                                                'X-ALT-DESC;FMTTYPE=text/html: ' + body,
                                                                                'SUMMARY: ' + subject,
                                                                                'ORGANIZER:MAILTO: ' + fromAddress,
                                                                                'ATTENDEE;CN="' + displayName + '";RSVP=TRUE:mailto: ' + toAddress,
                                                                                'BEGIN:VALARM',
                                                                                'TRIGGER:-PT15M',
                                                                                'ACTION:DISPLAY',
                                                                                'DESCRIPTION:Reminder',
                                                                                'END:VALARM',
                                                                                'END:VEVENT',
                                                                                'END:VCALENDAR'
                                                                                };
        String attachment = String.join(icsTemplate, '\n');
        return Blob.valueof(attachment ); 
    }
}

Just using plain text content here but can easily expand to use HTML in email and attachment.

Do mark this as a solution so that it helps other looking around.

This was selected as the best answer
AlexRitsonAlexRitson

Dear VBS,

 

Thank you so much for this!  Apologies, I have been in meetings all day and only just found the email.  I can't wait to check this out - will report back shortly.

 

I really appreciate you taking the trouble to help me.

 

Have a great weekend and best regards,

 

Alex.

AlexRitsonDevAlexRitsonDev

Dear VBS,

 

Frustratingly, for reasons I don't understand, the post you kindly put here yesterday has disappeared, along with my responses.

 

Could you possibly repost the updated code you created please?

 

For the record (i.e. for people who are searching this in future who need to understand the thread) what I put here yesterday which you responded to was this:

 

"

Once again, many thanks for your help with this.

 

Two problems, both of which are due to my inexperience.

 

Firstly, I now realise that I need to write an Apex trigger to accompany this.

 

I'd like the trigger to be an After trigger, which fires when the Opportunity Stage is advanced to "StageName 2. Pitch team/rehearsals confirmed"

 

Secondly, I'm not quite sure how to personalise the code you have so kindly written.

 

For the date/time of the meeting, I'd like the code to use the custom date/time field I have created  "First_Meeting__c"

 

The recipients should be 

 

$User.Email

 

Plus three Opportunity custom email fields

"IPA_Pitch_Member_1__c"

"IPA_Pitch_Member_1__c"

"IPA_Pitch_Member_1__c"

 

I'd also like to be able to cc cc'd to richard.patient@indigopa.com and alex,ritson@indigopa.com

 

Please could you help me with this?  Thank you in advance - I am tremendously grateful.

 

AlexRitsonAlexRitson
Dear VBS (and all who might find this code useful in future).
 
Something peculiar happened on this page; VBS kindly posted me the original code you see above; I needed some small modifications and s/he kindly made them and posted them here, then all those updated answers disappeared.  The reason why is a mystery!
Anyway, with the help of the Salesforce team, I have the code that VBS posted - and it is here:
 
I will need to make some further tweaks in the coming days and will message again.
 
BTW - I have two logons, for reasons of Salesforce functionality - AlexRitson and AlexRitsonDev.  Searching for AlexRitson brings up both.
 
Thanks for all your help and advice,
 
Alex.
public class SendEmailwithICSAttachment { 
public static void Sendmail_With_IcsAttachment(DateTime startDate, DateTime endDate, String location, String body, String subject, String fromAddress, String displayName, List<String> toAddress, List<String> ccAddress) 
{ String attachmentName = 'Meeting.ics';
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage(); 
//Now we have to set the value to Mail message properties 
//Note Please change it to correct mail-id to use this in your application 
msg.setReplyTo(fromAddress);
msg.setSenderDisplayName(displayName); 
msg.setToAddresses(toAddress); 
msg.setCcAddresses(ccAddress);
// it is optional, only if required 
msg.setSubject(subject); 
msg.setPlainTextBody(body); 
// Create the attachment
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); 
efa.setFileName(attachmentName ); 
Blob b = doIcsAttachment(startDate, endDate, location, body, subject, fromAddress, displayName, toAddress);
efa.setBody(b);
msg.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); 
// Send the email you have created. 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg }); 

private static Blob doIcsAttachment(DateTime startDate, DateTime endDate, String location, String body, String subject, String fromAddress, String displayName, String toAddress)

// Now Contruct the ICS file 
String [] icsTemplate = new List<String> 
{'BEGIN:VCALENDAR', 'PRODID:-//Schedule a Meeting', 'VERSION:2.0', 'METHOD:REQUEST', 'BEGIN:VEVENT', 'DTSTART: ' + String.valueof(startDate), 'DTSTAMP: '+ String.valueof(DateTime.Now()), 'DTEND: ' + String.valueof(endDate), 'LOCATION: ' + location, 'UID: ' + String.valueOf(Crypto.getRandomLong()), 'DESCRIPTION: ' + body, 'X-ALT-DESC;FMTTYPE=text/html: ' + body, 'SUMMARY: ' + subject, 'ORGANIZER:MAILTO: ' + fromAddress, 'ATTENDEE;CN="' + displayName + '";RSVP=TRUE:mailto: ' + toAddress, 'BEGIN:VALARM', 'TRIGGER:-PT15M', 'ACTION:DISPLAY', 'DESCRIPTION:Reminder', 'END:VALARM', 'END:VEVENT', 'END:VCALENDAR' }; 
String attachment = String.join(icsTemplate, '\n'); 
return Blob.valueof(attachment ); 
}
}

 

ilewi121ilewi121
This is the code from @AlexRitson, but formatted for easier legibility:
public class SendEmailwithICSAttachment {

	public static void Sendmail_With_IcsAttachment(DateTime startDate, DateTime endDate, String location, String body, String subject, String fromAddress, String displayName, List<String> toAddress, List<String> ccAddress){ 
		String attachmentName = 'Meeting.ics';
		Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage(); 
		//Now we have to set the value to Mail message properties 
		//Note Please change it to correct mail-id to use this in your application 
		msg.setReplyTo(fromAddress);
		msg.setSenderDisplayName(displayName); 
		msg.setToAddresses(toAddress); 
		msg.setCcAddresses(ccAddress);
		// it is optional, only if required 
		msg.setSubject(subject); 
		msg.setPlainTextBody(body); 
		// Create the attachment
		Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); 
		efa.setFileName(attachmentName ); 
		Blob b = doIcsAttachment(startDate, endDate, location, body, subject, fromAddress, displayName, toAddress);
		efa.setBody(b);
		msg.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); 
		// Send the email you have created. 
		Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg }); 
	}
	private static Blob doIcsAttachment(DateTime startDate, DateTime endDate, String location, String body, String subject, String fromAddress, String displayName, String toAddress){ 
		// Now Contruct the ICS file 
		String [] icsTemplate = new List<String> {
			'BEGIN:VCALENDAR',
			'PRODID:-//Schedule a Meeting',
			'VERSION:2.0', 'METHOD:REQUEST',
			'BEGIN:VEVENT',
			'DTSTART: ' + String.valueof(startDate),
			'DTSTAMP: '+ String.valueof(DateTime.Now()),
			'DTEND: ' + String.valueof(endDate),
			'LOCATION: ' + location,
			'UID: ' + String.valueOf(Crypto.getRandomLong()),
			'DESCRIPTION: ' + body,
			'X-ALT-DESC;FMTTYPE=text/html: ' + body,
			'SUMMARY: ' + subject,
			'ORGANIZER:MAILTO: ' + fromAddress,
			'ATTENDEE;CN="' + displayName + '";RSVP=TRUE:mailto: ' + toAddress,
			'BEGIN:VALARM',
			'TRIGGER:-PT15M',
			'ACTION:DISPLAY',
			'DESCRIPTION:Reminder',
			'END:VALARM',
			'END:VEVENT',
			'END:VCALENDAR' }; 
		String attachment = String.join(icsTemplate, '\n'); 
		return Blob.valueof(attachment ); 
	}
}

 
ilewi121ilewi121
This will attach the ICS file, but it doesn't display in the recipient's email handler as a calendar invite. Has anyone figured out how to do this?
See my post for more details: https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AsPlIAK
krish@123krish@123
HI 

Could You please share visuaforce page anad class for above code

we have requirement with my client 

and also Please suggent me how to put custom fields from contact object in the above code


Thanks
Olli HuhtalaOlli Huhtala
Hello!

Question related to this:

Is it possible to have this .ics generated to the OOTB functionality that sends the meeting summary?
Also, here's the idea which should've been implemented many years ago already imho:
https://success.salesforce.com/ideaView?id=08730000000BqoQAAS

BR,
Olli H.