Confirming date is within maintenance window

EstherJ
Tera Contributor

I have a catalog item for an end-user to request a change request. I'm trying to validate that the dates they provide are within a Schedule named "Data Center Maintenance", both start and end dates. This Schedule has a single Schedule Entry of every Sunday from 3am-11am, and no Child Schedules. All users and the system have the same date format. in_maintenance_window is always evaluating as false. Any hints on where I'm going wrong would be appreciated. Any suggestions for a cleaner way to do this would be appreciated. (Users can still submit their request outside the maintenance window, but in_maintenance_window = false triggers other required approvals.)

 

I have a Client Callable Script Include:

var MaintenanceWindowUtil = Class.create();
MaintenanceWindowUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isWithinWindow: function() {
        var startStr = this.getParameter('sysparm_start');
        var endStr = this.getParameter('sysparm_end');

        if (!startStr || !endStr) return 'false';

        var startDT = new GlideDateTime(startStr);
        var endDT = new GlideDateTime(endStr);

        var schedSysId = '4bf97fef1b9887105a97a64ce54bcbf0'; // Data Center Maintenance Schedule Sys ID
        var sched = new GlideSchedule(schedSysId);
       
        // Check if the window is completely within the schedule
        if (sched.appliesRange(startDT,endDT)) {
              return 'true';
        }
        return 'false';
    },
    type: 'MaintenanceWindowUtil'
});
 
Then I have Catalog Client Script on end_date of:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var start = g_form.getValue('start_date');
    var end = g_form.getValue('end_date');

    if (start && end) {
        g_form.hideFieldMsg('end_date'); // Clean up old errors
        // Check that End Date is after Start Date ... all users are configured to have same date format
        if (start >= end) {
            g_form.showFieldMsg('end_date', 'End date must be after start date', 'error');
            return;
        }
        var ga = new GlideAjax('MaintenanceWindowUtil');
        ga.addParam('sysparm_name', 'isWithinWindow');
        ga.addParam('sysparm_start', start);
        ga.addParam('sysparm_end', end);

        ga.getXMLAnswer(function(answer) {
            // Set a hidden variable 'in_maintenance_window' to true or false
            g_form.setValue('in_maintenance_window', answer);
 
            if (answer == 'true') {
                g_form.clearMessages();
            } else {
                g_form.showFieldMsg('end_date', 'Selected dates are outside the maintenance window', 'error');
            }
        });
    }
}
1 REPLY 1

Rafael Batistot
Kilo Patron

hi @EstherJ 

 

Please, change this part

 

if (sched.appliesRange(startDT,endDT)) {
return 'true';
}

To this one

 

if (sched.isInSchedule(startDT) && sched.isInSchedule(endDT)) {
return 'true';
}

If this response was helpful, please mark it as Helpful and, if applicable, as Correct, this helps other users find accurate and useful information more easily.