This is a basic widget for the ServiceNow Service Portal that implements fullcalendar.io - this allows for dynamic event creation, on-click event actions, and calendar styling.
Add required dependencies.
<div>
<!-- This div is a placeholer for the calendar (see Client Script) -->
<div id="calendar"></div>
</div>
/* Override existing calendar CSS. Find these classes in Google Inspect */
.fc td,.fc th {
border-color: #309C42;
background-color: #ffffff;
}
.fc-left {
color: #212121;
font-family: 'GT Super Text';
}
.fc button {
border-color: #309C42;
background-color: #ffffff;
}
api.controller=function() {
/* widget controller */
var c = this;
// Load fullcalendar.io calendar with options
$("#calendar").fullCalendar({
events: c.data.eventList,
// Set default event color
eventColor: '#344C38',
// Do something when clicked
eventClick: function (event) {
alert("Give some sort of alert/message");
// Redirect to page
top.window.location = 'https://github.com/ben-meeker/servicenow-calendar-widget';
}
});
};
(function() {
data.eventList=[];
// Get events from table
var events = new GlideRecord('incident')
// Add queries to narrow down data
events.addQuery('date>=javascript:gs.beginningOfLastMonth()')
events.query()
while (events.next()) {
var eventJSON = {};
// Set event JSON fields with useful data
// Set title of event
eventJSON.title = events.getValue('short_description');
// Set date field to tell calendar when to display event
eventJSON.date = events.getValue('due_date');
// Set allDay to true to remove time display on event in calendar
eventJSON.allDay = true;
// Set custom fields for specific use cases
eventJSON.sys_id = events.getValue('sys_id');
eventJSON.assigned_to = events.getValue('assigned_to');
// Set additional fields based on criteria
if (events.getValue('assigned_to') == false) {
eventJSON.color = 'white';
eventJSON.textColor = '#344C38';
eventJSON.borderColor = '#344C38';
}
data.eventList.push(eventJSON);
}
})();