Saturday 6 July 2013

ICS File In Salesforce

Have you ever tried sending a meeting Invitation from Salesforce using Apex Code. Below is the code to Achieve the Same.

It creates and sends an email. For any Calendar meeting the universal format for file Accepted is .ICS
and it is nothing but a string containing all the information.

I have used this file as the mail attachment and have set the Content type of email as "text/calendar"

Code Below
--------------------------------------------------------------------------------------------------------------------------

String vCal = 'BEGIN:VCALENDAR' + '\n' + 'PRODID:-//Force.com Labs//iCalendar Export//EN' + '\n' +
'VERSION:2.0' + '\n' + 'CALSCALE:GREGORIAN' + '\n' + 'METHOD:REQUEST'+ '\n'+ 'BEGIN:VEVENT'+ '\n' +
'DTSTART:20131008T103000Z' + '\n' + 'DTEND:20131008T113000Z' + '\n' + 'DTSTAMP:20091008T103839Z' + '\n' +
'ORGANIZER;CN=varun.vatsa@gmail.com:mailto:varun.vatsa@gmail.com' + '\n' + 'UID:varun.vatsa@gmail.com'+ '\n' +
'CREATED:20091008T103839Z' + '\n' + 'DESCRIPTION:something' + '\n' + 'LAST-MODIFIED:20091008T103839Z' + '\n' +
'SEQUENCE:0' + '\n' + 'STATUS:CONFIRMED' + '\n' + 'SUMMARY:Test ICS Mail Format' + + '\n' + 'TRANSP:OPAQUE' + '\n' +
'END:VEVENT'+ '\n' + 'END:VCALENDAR';
List<String> toAddresses = new List<String>();
        toAddresses.add('varun.vatsa@gmail.com');
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setSubject('Test');
        email.setToAddresses(toAddresses);
        email.setHtmlBody('Test');
        email.setPlainTextBody('Test');
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        efa.setFileName('rfc2445.ics');
     
        efa.setBody(blob.valueOf(vCal));
        //attachments.add(efa);
        efa.setContentType('text/calendar');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
         Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});

6 comments:

  1. Hello ,
    How can we set the timezone?
    Thanks for your response

    ReplyDelete
    Replies
    1. Hi Salma, You see, all the dates are in datetime format, so you may change the time zone :20091008T103839Z in the last three characters.

      Delete
  2. Hi,
    Do you think that is possible to send an .ics file to all the person inviteed on an event?
    I was thinkin that your article can help but i'm lost .
    Thanks for your responnse

    ReplyDelete
  3. Great Solution varun & Thanks for sharing it.

    ReplyDelete
  4. Hi Varun,

    I have generated ICS file from apex and sent over an email, but I am getting time difference which I have put in ICS file and Which is actually get in Attachment

    ReplyDelete
  5. Thank You and that i have a super proposal: Where To Learn Home Renovation house renovation before after

    ReplyDelete