GlideSchedule Not Returning Expected Time for Schedule Check — Always Outside Schedule

raj12345
Tera Contributor

Hi everyone,

I'm working on a script to check if the current time is within a specific schedule (8-5 weekdays excluding holidays) and create an HR case if it’s outside the schedule. I’m using GlideSchedule with the add() method and comparing the result with the current time to determine if we are within the schedule.

 

(function() {
// Load the schedule record from cmn_schedule by name
var schedGR = new GlideRecord('cmn_schedule');
if (!schedGR.get('name', '8-5 weekdays excluding holidays')) {
gs.error('Schedule not found: 8-5 weekdays excluding holidays');
return;
}

// Load the schedule using GlideSchedule or Java fallback
var schedule;
if (typeof GlideSchedule != 'undefined') {
schedule = new GlideSchedule(schedGR.sys_id);
} else {
schedule = new Packages.com.glide.schedules.Schedule(schedGR.sys_id);
}

// Current datetime
var now = new GlideDateTime();
now.setDisplayValue(gs.nowDateTime());

// Check if the current time is within the schedule window
var oneSecond = new GlideDuration(1000); // 1 second duration
var resultTime = schedule.add(now, oneSecond, '');

// If adding 1s shifts to a different time, we're outside schedule
if (!now.equals(resultTime)) {
// Proceed with case creation
var caseGR = new GlideRecord('sn_hr_core_case');
caseGR.initialize();
caseGR.short_description = 'Auto-created Daily Audit - Address Change';
caseGR.description = 'Automatically created case as part of daily audit.';
caseGR.hr_service = 'd578432212654d0e93733ada1e0dXXXX'; // Tax Payroll Support
caseGR.u_topic = 'tax_address_daily';
caseGR.assignment_group = 'XXXX';
caseGR.case_type = 'complex';
caseGR.insert();
}
})();

 

Please assist me in this.

Thanks

 

5 REPLIES 5

@raj12345 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader