Blog

Finding Incidents Outside of a Schedule

Here is some code you can use to find incidents outside a certain schedule.  In this example, it finds overnight incidents against a schedule, but you can adjust this code to meet your needs as well:

Script Include: findOvernightIncidents

Client Callable: true
Script:
function findOvernightIncidents() {
var schedRec = new GlideRecord('cmn_schedule');
schedRec.get('name', '8-5 weekdays excluding holidays');
//ServiceNow Version check for schedules
gs.log('Schedule Found: '+ schedRec.name)
if (typeof GlideSchedule != 'undefined') {
var sched = new GlideSchedule(schedRec.sys_id);
//gs.log('New Script Include Used: GlideSchedule')
}
else
{
var sched = new Packages.com.glide.schedules.Schedule(schedRec.sys_id);
//gs.log('Old Package Used: Packages.com.glide.schedules.Schedule')
}
//Find Incidents in Schedule
var incidents = [];
var grIncident = new GlideRecord('incident');
grIncident.addQuery('opened_at','>=',gs.daysAgoStart(1));
grIncident.query();
//gs.log('grIncident Query: ' + grIncident.getEncodedQuery() + ' = ' + grIncident.getRowCount());
while (grIncident.next()){
var opened_at = new GlideDateTime(grIncident.opened_at.getDisplayValue());
gs.log('Incident Number: '+grIncident.number+' Opened at: '+grIncident.opened_at.getDisplayValue() +'is in schedule? '+sched.isInSchedule(opened_at));
if (sched.isInSchedule(opened_at) == true) {
incidents.push(grIncident.number.toString());
}
}
gs.log('Incidents Outside of Schedule Yesterday: '+incidents);
return incidents;
}

Report: Overnight Incidents

Table: Incident
Filter: Number is javascript:findOvernightIncidents()