I wanted to share this potentially useful tip related to ServiceNow schedules. There might be instances when you need to use schedules, such as managing SLAs that should only trigger during work hours.
Setting up schedules is generally straightforward, but sometimes you need to exclude specific dates like bank holidays. The UK government has provided a JSON formatted list of UK bank holidays, accessible here:
https://www.api.gov.uk/gds/bank-holidays/#bank-holidays
This resource is really useful for populating bank holidays automatically. I created a new schedule, populate it with entries from the JSON data, and then referenced this schedule. If you’d like to use the same approach, here’s the script I used. You only need to replace ‘schedule_sys_id’ with the sys_id of your new schedule.
// SysID of the schedule to be updated var schedule_sys_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // Create a new instance of RESTMessageV2 for HTTP requests var request = new sn_ws.RESTMessageV2(); // Set the endpoint of the HTTP request request.setEndpoint('https://www.gov.uk/bank-holidays.json'); // Set the method to GET to fetch data request.setHttpMethod('GET'); // Set the header to accept application/json request.setRequestHeader("Accept","application/json"); // Execute the HTTP request and store the response var response = request.execute(); gs.info('HTTP request sent to fetch bank holidays'); // Get the response body var str = response.getBody(); // Parse the JSON string into a JavaScript object var obj = JSON.parse(str); // Extract the list of holidays for England and Wales var ukHolidays = obj['england-and-wales'].events; gs.info('Fetched ' + ukHolidays.length + ' UK bank holidays'); // Loop over each bank holiday for (var bankHoliday in ukHolidays) { // Check if the current property belongs to the object itself if(!ukHolidays.hasOwnProperty(bankHoliday)) continue; // Extract the title, date, and notes of the bank holiday var title = ukHolidays[bankHoliday].title; var date = ukHolidays[bankHoliday].date; var notes = ukHolidays[bankHoliday].notes; // Convert the date into a GlideDateTime object var gdt = new GlideDateTime(date); // Query the 'cmn_schedule_span' table to check if the record already exists var checkFirst = new GlideRecord('cmn_schedule_span'); checkFirst.addQuery('schedule', schedule_sys_id); checkFirst.addQuery('name',(title + ' - ' + gdt.getYearUTC())); checkFirst.query(); // If the record doesn't exist, create a new one if (!checkFirst.hasNext()){ var gr = new GlideRecord('cmn_schedule_span'); gr.initialize(); gr.schedule = schedule_sys_id; gr.name = (title + ' - ' + gdt.getYearUTC()); gr.type = 'exclude'; gr.show_as = 'busy'; gr.all_day = true; gr.start_date_time = gdt.getDate().toString(); gdt.addDays(1); gr.end_date_time = gdt.getDate().toString(); gr.notes = notes; gr.insert(); gs.info('Added bank holiday: ' + title + ' - ' + gdt.getYearUTC()); } else { gs.warn('Bank holiday already exists in the schedule: ' + title + ' - ' + gdt.getYearUTC()); } }