Create Calendar Entry with SSJS

/**
 * @class
 * A class to create, update, delete appointments in the users mailfile 
 * 
 * @description
 *  
 * @example
 * var ce = new CEntry();
 * ce.setUser("Ulrich Krause/SINGULTUS");
 * ce.setSubject('TEST Appointmrnt');
 * ce.setAppType('0');
 * ce.setLocation('Office Ratingen');
 * ce.setDTStart(session.createDateTime("11.01.2012 11:15:00"));
 * ce.setDTEnd(session.createDateTime("11.01.2012 13:45:00"));
 * return ce.createEntry();
 **/

function CEntry() {
	/**
	 * @constant
	 */	
	this.className='CEntry';
	/**
	 * @constant
	 */
	this.form='Appointment';
	
	this._subject = '';
	this._dtStart;
	this._dtEnd;
	this._user= '';
	this._appType = '0'; //DEFAULT = APPOINTMENT
	this._location = '';
	this._categories;

	/**
	 * @description
	 * sets AppointmentType, must be integer
	 * 
	 * @example
	 * 0 = APPOINTMENT, Termin
	 * 1 = ANNIVERSARY, Jahrestag
	 * 2 = EVENT, Ganztägige veranstaltung
	 * 3 = MEETING, Besprechung
	 * 4 = REMINDER, Erinnerung
	 * 
	 */
	this.setAppType =  function (appType:String) {
		try {			
			this._appType = AppType;
		} catch (e) {
			this._appType = '0';
		}
	}
	
	/**
	 * @description
	 * get AppointmentType
	 * 
	 * @return AppointmentType Integer
	 */
	this.getAppType =  function () {
		return this._appType;
	}
	
	/**
	 * @description
	 * set categories item
	 * 
	 * @param		categories String
	 */
	this.setCategories =  function (categories) {
		try {			
			this._categories = categories;
		} catch (e) {
			this._categories = '';
		}
	}
	
	/**
	 * @description
	 * get categories
	 * 
	 * @return		catagories String
	 */
	this.getCategories =  function () {
		return this._categories;
	}
	
	/**
	 * @description
	 * get Appointment Start
	 * 
	 * @return appointment start NotesDateTime
	 */
	this.getDTStart =  function () {
		return this._dtStart;
	}

	/**
	 * @description
	 * set Appointment Start
	 * 
	 * @param StartDT start NotesDateTime
	 */
	this.setDTStart =  function (StartDT:NotesDateTime) {
		try {
			this._dtStart = StartDT;
		} catch (e) {
			this._dtStart = @Now();
		}
	}
		
	/**
	 * @description
	 * get Appointment End
	 * 
	 * @return appointment start NotesDateTime
	 */
	this.getDTEnd =  function () {
		return this._dtEnd;
	}

	/**
	 * @description
	 * set Appointment End
	 * 
	 * @param dtEnd start NotesDateTime
	 */	
	this.setDTEnd =  function (dtEnd:NotesDateTime) {
		try {
			this._dtEnd = dtEnd;
		} catch (e) {
			this._dtEnd = @Now();
		}
	}	

	/**
	 * @description
	 * get Appointment Subject
	 * 
	 * @return		subject String
	 */
	this.getSubject =  function () {
		return this._subject;
	}
	
	/**
	 * @description
	 * set Appointment Subject
	 * 
	 * @param		subject String
	 */	
	this.setSubject =  function (subject:String) {
		try {
			this._subject = subject;
		} catch (e) {
			this._subject = '';
		}
	}
	
	/**
	 * @description
	 * set Appointment Member/User
	 * 
	 * @return		user String
	 */	
	this.getUser =  function () {
		return this._user;
	}
	
	/**
	 * @description
	 * set Appointment Member/User
	 * 
	 * @param		user String
	 */		
	this.setUser =  function (user:String) {
		try {
			this._user = user;
		} catch (e) {
			this._user = '';
		}
	}

	/**
	 * @description
	 * get Appointment Location
	 * 
	 * @return		Location String
	 */	
	this.getLocation =  function () {
		return this._location;
	}
	
	/**
	 * @description
	 * set appointment location
	 * 
	 * @param		Location String
	 */		
	this.setLocation =  function (location:String) {
		try {
			this._location = location;
		} catch (e) {
			this._location = '';
		}
	}
	
	/**
	 * @description
	 * get users mailfile server
	 * 
	 * @return		mailfile server String
	 */		
	// 
	this.getMailFileServer = function() {
	try {
			var notesdir:NotesDirectory;
			notesdir = session.getDirectory();
			return notesdir.getMailInfo(this._user, false, false).get(0);
	} catch (e) {
			return '';
			}
	}
	
	/**
	 * @description
	 * get users mailfile
	 * 
	 * @return		mailfile String
	 */		
	// 
	this.getMailFile = function() {
		try {
			var notesdir:NotesDirectory;
			notesdir = session.getDirectory();
			return notesdir.getMailInfo(this._user, false, false).get(3);
		} catch (e) {
			return '';
		}
	}

	/**
	 * @description
	 * does an appointment already exist in the target database / user calendar
	 * 
	 * @param		apptUNID String
	 * @return 		Boolean (true|false)
	 * @throws 		exception Integer 2 = cannot open DB or DB does not exist, 4 = general error / error executing code
	 */	
	this.exists = function(apptUNID){
		try {
			var db:NotesDatabase = session.getDatabase(this.getMailFileServer(), this.getMailFile(), false);
			if (db == null) {
				return '2'; // cannot open DB or DB does not exist
			} else {
				var v:NotesView = db.getView('($ApptUNID)');
				if (null == v) return '3'; //view not found
				var doc:NotesDocument = v.getDocumentByKey(apptUNID);
				return (null!=doc)?true:false;
			}
		} catch(e) {
			return '4'; //error
		}
	}

	/**
	 * @description
	 * update an existing entry in the target database / user calendar
	 * 
	 * @param		apptUNID String
	 * @return 		Integer ()
	 * @throws 		exception Integer 2 = cannot open DB or DB does not exist, 
	 */	
	this.updateEntry = function(apptUNID:String) {
		try {
			var db:NotesDatabase = session.getDatabase(this.getMailFileServer(), this.getMailFile(), false);
		if (db == null) {
			return '2';
		} else {
			var v:NotesView = db.getView('($ApptUNID)');
			if (null == v) return '3'; //view not found
			var doc:NotesDocument = v.getDocumentByKey(apptUNID);
				if ( null != doc) {
					doc.replaceItemValue('$NoPurge', this._dtEnd);
					//----------- Set Date and Times ----------------
					doc.replaceItemValue('calendarDateTime', this._dtStart );
					doc.replaceItemValue('StartDateTime', this._dtStart );
					doc.replaceItemValue('StartDate', this._dtStart );
					doc.replaceItemValue('StartTime', this._dtStart );
					doc.replaceItemValue('EndDateTime', this._dtEnd );
					doc.replaceItemValue('EndDate', this._dtEnd );
					doc.replaceItemValue('EndTime', this._dtEnd );	
					//----------- Other Items ----------------
					doc.replaceItemValue('Categories', this._categories);
					doc.replaceItemValue('Location', this._location);
					doc.replaceItemValue('Subject', this._subject);
					//----------- Save  ----------------
					doc.save(false, false);
					return '0'; // success
				} else {
					return '1' // cannot update document
				}
		}
		} catch (e) {
			_dump(e);
			return '2';
		}
	} 

	/**
	 * @description
	 * delete an existing entry in the target database / user calendar
	 * 
	 * @param		apptUNID String
	 * @return 		Integer ()
	 * @throws 		exception Integer 2 = cannot open DB or DB does not exist, 
	 */	
	this.deleteEntry = function(apptUNID:String) {
		try {
			var db:NotesDatabase = session.getDatabase(this.getMailFileServer(), this.getMailFile(), false);
			if (db == null) {
					return '2';
			} else	{
				var v:NotesView = db.getView('($ApptUNID)');
				if (null == v) return '3'; //view not found
				var doc:NotesDocument = v.getDocumentByKey(apptUNID);
				if ( null != doc) {
					doc.remove(true);
					v.refresh();
					return '0'; // success
				} else {
					return '1' // cannot remove document
				}
			}
		} catch(e) {
			_dump(e);
			return '2'; //operation failed
		}
	} 
	
	/**
	 * @description
	 * create a new entry in the target database / user calendar
	 * 
	 * @param		apptUNID String
	 * @return 		Integer ()
	 * @throws 		exception
	 * 1 'No MailFile or User not found
	 * 2 'MalFile cannot be opened 
	 * 3 'Subject missing
	 * 4 'EndDT before StartDT
	 * 5 'User missing
	 */		
	this.createEntry = function(apptUNID:String) {
		if (this._subject == "") return '3';
		if (this._user == "") return '5';
		if (this.getMailFile() == '') return '1';
		try {
			var db:NotesDatabase = session.getDatabase(this.getMailFileServer(), this.getMailFile(), false);
			if (db == null) {
				return '2';
			} else {
				var nam:NotesName = session.createName(this._user);
				var doc:NotesDocument = db.createDocument();
				this._apptUNID = ('' == apptUNID || null == apptUNID)?doc.getUniversalID():apptUNID;
				doc.replaceItemValue('Form', this.form);
				doc.replaceItemValue('$Programmatically', '1');
				doc.replaceItemValue('$BusyPriority', '1');
				doc.replaceItemValue('Logo', 'stdNotesLtr0');
				doc.replaceItemValue('OrgState', 'x');
				doc.replaceItemValue('Repeats', '');
				doc.replaceItemValue('Resources', '');
				doc.replaceItemValue('Categories', this._categories);
				doc.replaceItemValue('SaveOptions', '');
				doc.replaceItemValue('APPTUNID', this._apptUNID);
				doc.replaceItemValue('OrgTable', 'C0');
				doc.replaceItemValue('$PublicAccess', '1');
				doc.replaceItemValue('MailOptions', '');
				doc.replaceItemValue('$NoPurge', this._dtEnd);
				doc.replaceItemValue('AppointmentType', this._appType);
				doc.replaceItemValue('Subject', this._subject);
				doc.replaceItemValue('Location', this._location);
				//----------- Set Date and Times ----------------
				doc.replaceItemValue('calendarDateTime', this._dtStart );
				doc.replaceItemValue('StartDateTime', this._dtStart );
				doc.replaceItemValue('StartDate', this._dtStart );
				doc.replaceItemValue('StartTime', this._dtStart );
				doc.replaceItemValue('EndDateTime', this._dtEnd );
				doc.replaceItemValue('EndDate', this._dtEnd );
				doc.replaceItemValue('EndTime', this._dtEnd );		
				//----------- Author Items ----------------
				var item:NotesItem = null;
				item = doc.appendItemValue('From', nam.getCanonical());
				item.isAuthors();
				item = doc.appendItemValue('Principal', nam.getCanonical());
				item.isAuthors();				
				item = doc.appendItemValue('$BusyName', nam.getCanonical());
				item.isAuthors();		
				//----------- Exclude From View ----------------
				item = doc.appendItemValue('ExcludeFromView', 'D');
				item.appendToTextList('S')
				//----------- View Icon ----------------
				switch (this._appType) {
				case '0':
					item = doc.appendItemValue('_ViewIcon', 160);
					break;
				case '1':
					item = doc.appendItemValue('_ViewIcon', 160);
					break;
				case '2':
					item = doc.appendItemValue('_ViewIcon', 168);
					break;
				case '3':
					item = doc.appendItemValue('_ViewIcon', 9);
					break;			
				case '4':
					item = doc.appendItemValue('_ViewIcon', 158);
					break;	
				default: 
					item = doc.appendItemValue('_ViewIcon', 160);
				}
				item.isSummary();
                //----------- Alarm Options ----------------
                //doc.replaceItemValue('$Alarm', 1);
                //doc.replaceItemValue('$AlarmOffset', -3);
                //doc.replaceItemValue('$AlarmMemoOptions', '2'); //Send notification with subject
                //doc.replaceItemValue('$AlarmSendTo', names);
                //doc.replaceItemValue('$AlarmSound', 'chimes');
                //doc.replaceItemValue('$AlarmUnit', 'M');
				doc.computeWithForm(true, false);
				doc.save(false, false);
				return this._apptUNID;
			}
		} catch(e) {
			dBar.error(e);
			return '2';
		}
	} 
}





Create Appointments in mail files. LS Version here http://openntf.org/XSnippets.nsf/snippet.xsp?id=create-calendar-entry-with-lotusscript

JavaScript (Server)
Ulrich Krause
May 18, 2012 10:39 PM
Rating
313

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



1 comment(s)Login first to comment...
Chintan Parekh
(at 05:15 on 17.01.2014)
Hi,

This is excellent. However, I am not able to figure out how to delete or say edit an existing entry. Can you please help me out here.