Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

if incident is raised out of the working hrs a call out mechanism to be triggered (mail)

monishasp
Tera Contributor

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
    }
})();

1 REPLY 1

SwamyM
Giga Guru
Giga Guru

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");
}
 
 
Please select Correct or Helpful options if my answer is useful.
 
Regards,
Swamy