Confirming date is within maintenance window

EstherJ
Tera Expert

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 ACCEPTED SOLUTION

Got some help from someone who knew someone, and he figured out it was Timezone related. Here's the "fixed" script, in case it helps anyone else.

 

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();
        var endDT = new GlideDateTime();
        startDT.setDisplayValue(startStr);
        startDT.setTimeZone('US/Eastern');
        endDT.setDisplayValue(endStr);
        endDT.setTimeZone('US/Eastern');
 
        var schedSysId = '4bf97fef1b9887105a97a64ce54bcbf0'; // Data Center Maintenance Schedule Sys ID
        var sched = new GlideSchedule(schedSysId);
        sched.setTimeZone('US/Eastern');
        // Checks if the window is completely within the schedule
        var checkStart = sched.isInSchedule(startDT);
        var checkEnd = sched.isInSchedule(endDT);

        if (checkStart && checkEnd) {
            return 'true';
        } else {
            return 'false';
        }
    },
    type: 'MaintenanceWindowUtil'
});
 
 
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var start = g_form.getDisplayValue('start_date');
    var end = g_form.getDisplayValue('end_date');

    if (start && end) {
        g_form.hideFieldMsg('end_date'); // Clean up old errors
        // Check that End Date is after Start Date
        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
            // Show message based on result
            if (answer === 'true') {
                g_form.setValue('in_maintenance_window', true);
                g_form.clearMessages();
            } else if (answer === 'false') {
                g_form.setValue('in_maintenance_window', false);
                g_form.showFieldMsg('end_date', 'Selected dates are outside the maintenance window', 'error');
            }
        });
    }
}

View solution in original post

3 REPLIES 3

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.

Hi, Rafeal.

 

That worked once, so I showed it to the colleague who reported the issue, and it went back to doing the everything is false again. It flips the flag to false as soon as end_date is selected.

 

Are there other things I need to check besides the Script Include, Client Script, and Schedule?

 

Thanks!

Esther

Got some help from someone who knew someone, and he figured out it was Timezone related. Here's the "fixed" script, in case it helps anyone else.

 

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();
        var endDT = new GlideDateTime();
        startDT.setDisplayValue(startStr);
        startDT.setTimeZone('US/Eastern');
        endDT.setDisplayValue(endStr);
        endDT.setTimeZone('US/Eastern');
 
        var schedSysId = '4bf97fef1b9887105a97a64ce54bcbf0'; // Data Center Maintenance Schedule Sys ID
        var sched = new GlideSchedule(schedSysId);
        sched.setTimeZone('US/Eastern');
        // Checks if the window is completely within the schedule
        var checkStart = sched.isInSchedule(startDT);
        var checkEnd = sched.isInSchedule(endDT);

        if (checkStart && checkEnd) {
            return 'true';
        } else {
            return 'false';
        }
    },
    type: 'MaintenanceWindowUtil'
});
 
 
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var start = g_form.getDisplayValue('start_date');
    var end = g_form.getDisplayValue('end_date');

    if (start && end) {
        g_form.hideFieldMsg('end_date'); // Clean up old errors
        // Check that End Date is after Start Date
        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
            // Show message based on result
            if (answer === 'true') {
                g_form.setValue('in_maintenance_window', true);
                g_form.clearMessages();
            } else if (answer === 'false') {
                g_form.setValue('in_maintenance_window', false);
                g_form.showFieldMsg('end_date', 'Selected dates are outside the maintenance window', 'error');
            }
        });
    }
}