Need to prevent user from selecting a past date greater than 7 days but not working as expected

Rhonda9
Tera Expert

I have the following requirement, and I am using a script include and client script  but I cannot get it to work.  What am I doing wrong.? 

  • Emergency Change Type:

    • If the selected date is more than 7 days in the past, submission should be prevented.
    • If the date is less than or equal to 7 days in the past, submission should be allowed, but a warning message should be displayed.
  • Normal or Standard Change Type:

    • Submission should be blocked if the selected date is any date in the past, with a validation error shown.

Client Script:

function onSubmit() {
    var changeType = g_form.getValue('type'); // 'normal', 'standard', 'emergency'
    var plannedStart = g_form.getValue('start_date');
    var plannedEnd = g_form.getValue('end_date');

    if (!plannedStart || !plannedEnd) {
        return true; // Let submission proceed if dates are missing
    }

    // Use GlideAjax to call a server script that calculates business days
    var ga = new GlideAjax('BusinessDaysChecker');
    ga.addParam('sysparm_name', 'checkDates');
    ga.addParam('sysparm_changeType', changeType);
    ga.addParam('sysparm_start', plannedStart);
    ga.addParam('sysparm_end', plannedEnd);

    var answer = ga.getXMLWait();
    var result = answer.documentElement.getAttribute('answer');
    var resObj = JSON.parse(result);

    if (resObj.action === 'abort') {
        g_form.showFieldMsg('start_date', resObj.message, 'error');
        return false; // Stop submission
    } else if (resObj.action === 'warn') {
        g_form.showFieldMsg('start_date', resObj.message, 'info');
        return true; // Allow submission
    }
    return true;
}

 

Script Include

var BusinessDaysChecker = Class.create();
BusinessDaysChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    checkDates: function () {
        var changeType = this.getParameter('sysparm_changeType');
        var startDate = new GlideDateTime(this.getParameter('sysparm_start'));
        var endDate = new GlideDateTime(this.getParameter('sysparm_end'));
        var now = new GlideDateTime();

        // Load your business schedule (replace with your sys_id)
        var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
        // Example: '08e3c5c60a0a0b26001f8d67b1d6b432' for default 8-5 M-F

        var daysPastStart = schedule.duration(startDate, now) / (1000 * 60 * 60 * 24);
        var daysPastEnd = schedule.duration(endDate, now) / (1000 * 60 * 60 * 24);

        var response = { action: 'allow', message: '' };

        if (changeType === 'normal' || changeType === 'standard') {
            if (daysPastStart >= 1) {
                response.action = 'warn';
                response.message = 'Planned start date is in the past.';
            }
        }
        else if (changeType === 'emergency') {
            if (daysPastStart > 7 || daysPastEnd > 7) {
                response.action = 'abort';
                response.message = 'Emergency change dates cannot be more than 7 business days in the past.';
            } else if (daysPastStart > 0 || daysPastEnd > 0) {
                response.action = 'warn';
                response.message = 'Emergency change dates are in the past.';
            }
        }

        return JSON.stringify(response);
    },

    type: 'BusinessDaysChecker'
});

 

3 REPLIES 3

kaushal_snow
Mega Sage

Hi @Rhonda9 ,

 

you are using 

 

var daysPastStart = schedule.duration(startDate, now) / (1000 * 60 * 60 * 24);

 

But,  schedule.duration() returns a GlideDuration object, not a number, so dividing it directly gives wrong results unless you call .getNumericValue(). Even if you fix that, duration() only counts business hours in your schedule, not total calendar days, and if you swap the parameters incorrectly it can return negative or zero when you expect a positive days in the past.

 

First, you need to fix the date difference calculation.

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

 

 

 

 

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

Viraj Hudlikar
Giga Sage

@Rhonda9 

var BusinessDaysChecker = Class.create();
BusinessDaysChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    checkDates: function () {
        var changeType = this.getParameter('sysparm_changeType');
        var startDate = new GlideDateTime(this.getParameter('sysparm_start'));
        var now = new GlideDateTime();
        
        var response = { action: 'allow', message: '' };

        // Check if the start date is in the past
        if (startDate.getNumericValue() < now.getNumericValue()) {
            
            // Calculate the difference in milliseconds and convert to days
            var diffMs = now.getNumericValue() - startDate.getNumericValue();
            var daysPast = Math.floor(diffMs / (1000 * 60 * 60 * 24));
            
            if (changeType === 'normal' || changeType === 'standard') {
                response.action = 'abort';
                response.message = 'Planned start date cannot be in the past for Normal or Standard changes.';
            } else if (changeType === 'emergency') {
                if (daysPast > 7) {
                    response.action = 'abort';
                    response.message = 'Emergency change dates cannot be more than 7 days in the past.';
                } else { // 0 to 7 days in the past
                    response.action = 'warn';
                    response.message = 'Emergency change dates are in the past. This will require special approval.';
                }
            }
        }
        
        return JSON.stringify(response);
    },

    type: 'BusinessDaysChecker'
});

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

 

Thank you for your response, it doesn't seem to be working for me.