Creating a calendar entry from an XPage event

One of the areas I struggled with in our demo was creating a calendar entry from the XPage component when the button was clicked in the previous post.  The problem is you need to make sure you include all of the right fields otherwise your calendar entry will not display and open properly in the Notes 8.5 calendar.  The key element I was missing was the”@GetCurrentTimeZone” as the computeWithForm() did not calculate all of the right fields when the note was saved.  For the most part it was straight forward, I had to parse out some of the information so the code looks hackish.

Source Code for event:

var dir = session.getDbDirectory( session.getServerName() );
var mail = dir.openMailDatabase();
var doc = mail.createDocument();
doc.appendItemValue("Form", "Appointment");
doc.appendItemValue("AppointmentType", "3");
doc.appendItemValue("From", session.getEffectiveUserName());
doc.appendItemValue("Principle", session.getEffectiveUserName());
doc.appendItemValue("Chair", session.getEffectiveUserName());
doc.appendItemValue("Subject", context.getSessionProperty("summary"));
doc.appendItemValue("Location", context.getSessionProperty("location"));
doc.appendItemValue("Categories", "Lotusphere 2010");
dt = context.getSessionProperty("begintime");
//Get the date out
date = dt.substr(0, 10);
//Now the time
begin = dt.indexOf("T", 10) + 1;
time = dt.substr(begin, 8);
d1 = session.createDateTime(date + " " + time);
doc.replaceItemValue("StartDateTime", d1);
doc.replaceItemValue("StartDate", d1);
doc.replaceItemValue("StartTime", d1);
tz = session.evaluate("@GetCurrentTimeZone");
doc.replaceItemValue("StartTimeZone", tz[0]);
doc.replaceItemValue("CalendarDateTime", d1);
dt = context.getSessionProperty("endtime");
date = dt.substr(0, 10);
//Now the time
begin = dt.indexOf("T", 10) + 1;
time = dt.substr(begin, 8);
d2 = session.createDateTime(date + " " + time);
doc.replaceItemValue("EndDateTime", d2);
doc.replaceItemValue("EndDate", d2);
doc.replaceItemValue("EndTime", d2);
doc.replaceItemValue("EndTimeZone", tz[0]);
var description = context.getSessionProperty("description");
var speakers = context.getSessionProperty("speakers");
body = "SPEAKERS:nn" + speakers + "nnDESCRIPTION:nn" + description;
doc.replaceItemValue("Body", body);
doc.computeWithForm(true, false);
doc.save(true, false);

3 thoughts on “Creating a calendar entry from an XPage event

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.