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 

sending an ICS (Outlook compatible) calendar request with workflow

Hello,

 

This is my first post here.  I'm putting in Salesforce to my organisation and it has been good so far.  However, there is one feature which is missing, and I really need to find a workaround.

 

During our Opportunitiy stage, we have various meetings.  Some of the people who attend are Salesforce users, most of the people aren't, but their emails are in the system and accessible to the either as contacts or custom email fields.  It is obviously easy to create a workflow rule that emails all the people I want to email when a meeting is arranged, however I want to accompany the email with an ICS attachment that has the meeting time.

 

I'm not a programmer, although I do play with HTML code in my spare time for fun.  As such, I may well ask things that appear a bit dumb - apologies in advance for this.

 

However, I have found the two following bits of code which might make my objective possible, with a bit of tweaking.  If you can give me any help or advice, I shall be most grateful.  I have also realised that I'm noto the only one who wants this goal, so if you can come up with the answer, there will be grateful people around the world.

 

Thanks in advance!

 

Alex.

 

CODE 1:

 

<?php
  $date      = $_GET['date'];  $startTime = $_GET['startTime'];  $endTime   = $_GET['endTime'];  $subject   = $_GET['subject'];  $desc      = $_GET['desc'];  $ical ="BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:". md5(uniqid(mt_rand(),true))."example.com
DTSTAMP:". gmdate('Ymd').'T'. gmdate('His')."Z
DTSTART:".$date."T".$startTime."00Z
DTEND:".$date."T".$endTime."00Z
SUMMARY:".$subject."
DESCRIPTION:".$desc."
END:VEVENT
END:VCALENDAR";//set correct content-type-header  header('Content-type: text/calendar; charset=utf-8');  header('Content-Disposition: inline; filename=calendar.ics');  echo $ical;exit;?>

CODE NO 2

 

publicvoid Sendmail_With_IcsAttachment()

        {

 

            MailMessage msg = newMailMessage();

            //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.From = newMailAddress("xxxxx@xyz.com", "ABC");

            msg.To.Add(newMailAddress("yyyyy@xyz.com", "BCD"));

            msg.CC.Add(newMailAddress("zzzzz@xyz.com", "DEF"));// it is optional, only if required

            msg.Subject = "Send mail with ICS file as an Attachment";

            msg.Body = "Please Attend the meeting with this schedule";

 

            // Now Contruct the ICS file using string builder

            StringBuilder str = newStringBuilder();

            str.AppendLine("BEGIN:VCALENDAR");

            str.AppendLine("PRODID:-//Schedule a Meeting");

            str.AppendLine("VERSION:2.0");

            str.AppendLine("METHOD:REQUEST");

            str.AppendLine("BEGIN:VEVENT");

            str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+330)));

            str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));

            str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", DateTime.Now.AddMinutes(+660)));

            str.AppendLine("LOCATION: " + this.Location);

            str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));

            str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));

            str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));

            str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));

            str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));

 

            str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));

 

            str.AppendLine("BEGIN:VALARM");

            str.AppendLine("TRIGGER:-PT15M");

            str.AppendLine("ACTION:DISPLAY");

            str.AppendLine("DESCRIPTION:Reminder");

            str.AppendLine("END:VALARM");

            str.AppendLine("END:VEVENT");

            str.AppendLine("END:VCALENDAR");

 

            //Now sending a mail with attachment ICS file.                     

            System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();

            smtpclient.Host = "localhost"; //-------this has to given the Mailserver IP

 

            smtpclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

 

            System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");

            contype.Parameters.Add("method", "REQUEST"); 

            contype.Parameters.Add("name", "Meeting.ics");

            AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), contype);

            msg.AlternateViews.Add(avCal);

            smtpclient.Send(msg); 

 

          

        }

 

Peter_sfdcPeter_sfdc
There is a previous post that seems to address this. Have you looked at this one?

http://boards.developerforce.com/t5/Apex-Code-Development/Email-MIME-Type-and-iCal-ICS-format/td-p/131143
AlexRitsonAlexRitson

Thank you very much for coming back to me so quickly - you are very kind.

 

I took the code in this answer, created a new custom object (Outlook Invitation) and then started the process of creating a new Apex class.

 

I copied the code in - and initially got an error message "Error: Compile Error: expecting a right parentheses, found 'rfc2445.ics' at line 4 column 24".

 

After a bit of googling, I found this answer to someone else's problem (http://boards.developerforce.com/t5/Apex-Code-Development/Error-Compile-Error-expecting-a-right-parentheses-for-Apex/td-p/467803)and from that, added this extra line:

 

        OutlookInvitation = new OutlookInvitation();

 

which appears to solve this initial error.

 

My code now reads:

 

public class OutlookInvitation {Messaging.EmailFileAttachment[] attachments = new Messaging.EmailFileAttachment[0];
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
OutlookInvitation = new OutlookInvitation();
efa.setFileName('rfc2445.ics');


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:20091008T103000Z' + '\n' + 'DTEND:20091008T113000Z' + '\n' + 'DTSTAMP:20091008T103839Z' + '\n' +
'ORGANIZER;CN=lan.pham@icwa.wa.gov.au:mailto:lan.pham@icwa.wa.gov.au' + '\n' + 'UID:lan.pham@icwa.wa.gov.au'+ '\n' +
'CREATED:20091008T103839Z' + '\n' + 'DESCRIPTION:something' + '\n' + 'LAST-MODIFIED:20091008T103839Z' + '\n' +
'SEQUENCE:0' + '\n' + 'STATUS:CONFIRMED' + '\n' + 'SUMMARY:Go Home Lan' + + '\n' + 'TRANSP:OPAQUE' + '\n' +
'END:VEVENT'+ '\n' + 'END:VCALENDAR';


efa.setBody(blob.valueOf(vCal));
attachments.add(efa);
efa.setContentType('text/calendar');

 

But I am getting the error message "Error: Compile Error: unexpected token: '=' at line 4 column 26"

 

Would greatly appreciate further advice.  Also, the post with the code I am copying that you suggested earlier ends with the scary words ".... do the rest of the email attachment code". Would the code I pasted originally serve this purpose - and if so, where do I put it?

 

My original idea was to have put all this in the HTML for a custom email template, but that's clearly not going to work.

 

Thank you for helping a newbie - sorry if I'm asking stupid things.