- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I'm using a client catalog script and and client callable script include to implement a requirement and avoid the selection of weekend days and holidays on a date time field on a catalog item.
I'm using the glideSchedule isInSchedule method but I'm getting a false when selecting a week day.
Here is the client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}
// Get today's date (without time)
var today = new Date();
today.setHours(0, 0, 0, 0);
// Get the selected date
var selectedDate = new Date(newValue);
selectedDate.setHours(0, 0, 0, 0);
// Calculate 3 days in milliseconds
var threeDaysMs = 3 * 24 * 60 * 60 * 1000;
// Minimum allowed date = today + 3 days
var minAllowedDate = new Date(today.getTime() + threeDaysMs);
// First validation: 3 days rule
if (selectedDate < minAllowedDate) {
g_form.clearValue('date_required');
g_form.showFieldMsg(
'date_required', 'Date required must be 3 days (or more) from the current date.', 'error');
return;
}
// SECOND VALIDATION: business days (Mon–Fri + excluding holidays)
var ga = new GlideAjax('DateBusinessValidator');
ga.addParam('sysparm_name', 'validateAndMinDate');
ga.addParam('sysparm_schedule', 'a4f73987db2e2340917fd7795e961951'); // sys id schedule
ga.addParam('sysparm_business_days', 3);
ga.addParam('sysparm_date', newValue);
ga.getXMLAnswer(function(answer) {
console.log("DEBUG → RAW ANSWER from Script Include:", answer);
try {
var res = JSON.parse(answer);
console.log("DEBUG → Parsed response:", res);
if (!res.isBusinessDay) {
console.warn("DEBUG → FAIL: isBusinessDay = false");
g_form.clearValue('date_required');
g_form.showFieldMsg('date_required', 'Selected date must be a business day (Mon–Fri, excluding holidays).', 'error');
} else {
console.log("DEBUG → PASS: isBusinessDay = true");
}
} catch (e) {
console.error("DEBUG → JSON PARSE ERROR:", e);
g_form.showFieldMsg('date_required', 'Error validating business day.', 'error');
}
});
}Here is the Script include
var DateBusinessValidator = Class.create();
DateBusinessValidator.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateAndMinDate: function() {
// Read parameters
var scheduleId = this.getParameter('sysparm_schedule');
var minBusinessDays = parseInt(this.getParameter('sysparm_business_days'), 10);
var dateStr = this.getParameter('sysparm_date');
// DEBUG: incoming parameters
gs.info("DEBUG SI → scheduleId: " + scheduleId);
gs.info("DEBUG SI → minBusinessDays: " + minBusinessDays);
gs.info("DEBUG SI → dateStr (raw): " + dateStr);
// Load schedule
var schedule = new GlideSchedule(scheduleId);
gs.info("DEBUG SI → schedule loaded? " + (schedule ? "YES" : "NO"));
// Force selected date to 09:00:00 to avoid timezone shifting
var selected = new GlideDateTime(dateStr + " 09:00:00");
gs.info("DEBUG SI → selected (with forced time): " + selected.getDisplayValue());
gs.info("DEBUG SI → selected (internal): " + selected.getValue());
// Check if selected date is a business day
var isBusiness = schedule.isInSchedule(selected);
gs.info("DEBUG SI → isBusinessDay: " + isBusiness);
// Calculate minimum allowed business date
var now = new GlideDateTime();
now.setDisplayValue(now.getLocalDate().toString() + " 09:00:00");
gs.info("DEBUG SI → now (forced 09:00): " + now.getDisplayValue());
var minDate = new GlideDateTime(now);
// Add business time: X days * 24h * 60m * 60s * 1000ms
var ms = minBusinessDays * 24 * 60 * 60 * 1000;
schedule.add(minDate, ms);
gs.info("DEBUG SI → minBusinessDate (calculated): " + minDate.getDisplayValue());
gs.info("DEBUG SI → minBusinessDate (local only): " + minDate.getLocalDate().toString());
// Build response
var result = {
isBusinessDay: isBusiness,
minBusinessDate: minDate.getLocalDate().toString()
};
gs.info("DEBUG SI → JSON returned: " + JSON.stringify(result));
return JSON.stringify(result);
}
});
here is my Schedule of 8 to 5 weekdays excluding holidays
Scheduled entry
I believe it could be because the "When" and "To" dates are in the past, however the schedule is set to repeat every weekday repeat until is empty.
Why I'm getting a false returned when selecting a week day in this case ?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Ramonrdd3
once you fix that schedule entry part highlighted by me below, check this link where it has solution
Business days Issue -> response from Dave_p
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Did you get it working? You can also just use an ui policy to handle the client side date restriction.
Also it might be simpler to make an all day workday schedule instead if you don't care about the working hours. And also make it floating tz so it uses the session timezone. Or you can set timezone on the gdts
Working with times is extremely difficult in my opinion but this is what I would do if i understand your requirement correctly, although i would probably have the n days in future restriction only on the client side
var date = new GlideDateTime(),
now = new GlideDateTime(),
schedule = new GlideSchedule("6bfda72083437210557ff0d6feaad315"),
daysFromNow = 3,
inputDate = "2026-02-15",
result = {
isDaysFromNow: false,
isBusinessDay: false,
minBusinessDate: ""
};
//set times at midnight session tz
now.setDisplayValue(now.getDate())
date.setDisplayValue(inputDate + " 00:00:00");
//is date x bizdays from now
now.addDays(daysFromNow);
result.isDaysFromNow = GlideDateTime.subtract(date, now).getNumericValue() <= 0;
//is in schedule aka business day
result.isBusinessDay = schedule.isInSchedule(date);
//find start of next business day 00:00:00 in session time
var untilNext = schedule.whenNext(date) / 1000;
date.addSeconds(untilNext);
result.minBusinessDate = date.getLocalDate();
gs.info(JSON.stringify(result, null, 2));
/*
*** Script: {
"isDaysFromNow": true,
"isBusinessDay": false,
"minBusinessDate": "2026-02-16"
}
*/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I figure out why i was gettin so many days in the future.
Turns out since the schedule is 8 to 5, when adding a duration of 3 days, it was actually adding 72 hours, which would add up additional days from human perspective. Something importan to know and consider.
I also confirmed what I suspected initially. The provided solution works fine for counting business days withing the time range, in my case 3 business days, in your solution 5 days.
But outside that range, it doesn't validate if a weekend day or holiday is selected, which i need. I know how to check if a sunday or saturday is selected using glidedatetime methods.
But with holidays can get tricky if i can't leverage schedules the way i though i was going to be able to.
Thanks for your assistance !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
schedule is 8 to 5 means 9 hours (1 business day) and 3 business days means 9*3=27 hours
Glad to help.
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader

