GlideSchedule isInSchedule not working

Ramonrdd3
Tera Expert

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

Ramonrdd3_0-1770763535992.png

Scheduled entry

Ramonrdd3_1-1770763584951.png

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 ?

0 REPLIES 0