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

iekosmadakis
Mega Sage

Hello @raj12345 !
You can use the method GlideSchedule.isInSchedule(). It’s simpler, faster and purpose-built for this check:

(function executeDailyAudit() {
	// 1. Grab the “8-5 weekdays excluding holidays” schedule
	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;
	}

	// 2. Evaluate 'now' against the schedule
    var sched = new GlideSchedule(schedGR.getUniqueValue());
	var nowGdt = new GlideDateTime();
	var inside  = sched.isInSchedule(nowGdt); // TRUE = within 8-5 weekday window

	// 3. Outside business hours -> raise the HR Case
	if (!inside) {
		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 consider marking my answer as helpful and accepting it as the solution if it assisted you in any way.

Thanks for support but This is something not working 

 

Please give me below attached ss of code . Working code

Hello @raj12345 ! Can you please let me know where it got stuck? Because I ran it in my PDI (with a different schedule, of course) without any issues. I suggest having a look at this line first and validating that the query returns the correct record schedule:

schedGR.get('name', '8-5 weekdays excluding holidays')

Ankur Bawiskar
Tera Patron
Tera Patron

@raj12345 

so what debugging did you do?

try this

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

    var nowDateTime = new GlideDateTime();

    var schedule = new GlideSchedule(schedGR.getUniqueValue());
    var inSchedule = schedule.isInSchedule(nowDateTime);

    if (inSchedule) {
        // your logic
        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();
    }

})();

Working logic for schedule check

    var nowDateTime = new GlideDateTime();

    var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828'); // I gave sysId of 8-5 weekdays excluding holidays 
    var inSchedule = schedule.isInSchedule(nowDateTime);
    gs.info(inSchedule);

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