if incident is raised out of the working hrs a call out mechanism to be triggered (mail)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2025 03:16 AM
HI,
I need assistance with setting up a mechanism to trigger notifications for incidents raised outside of working hours. I've created a schedule in the cmn_schedule table with working hours set from 8 AM to 5 PM. I attempted to build a notification system using this code, but I'm having trouble. Could someone help me with the code?
(function() {
// Define the schedule name for "8-5 weekdays excluding holidays"
var scheduleName = "8-5 weekdays excluding holidays"; // Use the exact name of your schedule
var schedule = new GlideSchedule(scheduleName);
// Get the creation time of the incident
var createdOn = current.sys_created_on;
// Get the next available working time based on the schedule
var nextWorkingTime = schedule.getNextScheduledTime(createdOn);
// Check if the incident creation time is outside of working hours
// If the incident was created before the start of working hours or after the end, it's considered outside
var incidentTime = createdOn.getDisplayValue(); // Convert to string for display purposes
// If the incident is created outside working hours (before 8 AM or after 5 PM), return true for the condition
if (createdOn < nextWorkingTime) {
return true; // This triggers the notification as the incident is outside working hours
} else {
return false; // Incident created during working hours, no notification
}
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2025 03:36 AM
Hi @monishasp ,
In your script you are not setting the timezone to schedule. Below is sample code to set your timezone.
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828', 'US/Pacific');
And instead of calculating nextschedule time, you can check whether current time is in Schedule window or not. Below is the sample script.
var glide = new GlideRecord('cmn_schedule'); glide.addQuery('type', 'blackout'); glide.query(); if (glide.next()) { var sched = new GlideSchedule(glide.sys_id); var date = new GlideDateTime(); date.setDisplayValue("2007-09-18 12:00:00"); if (sched.isInSchedule(date)) gs.info("Is in the schedule"); else gs.info("Is NOT in the schedule"); }
https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/app-store/dev_portal/API_referenc...