Issue with Client Callable Script Include and Client Script

0890KM
Tera Contributor

Hey All, 

 

There is something off with my script include and client script. I need the selected date & time field to be selected at least 2 business days after and not selected on weekend days. Right now, it doesn't matter what I put in the date & time field, it runs error message and clears value. What am I doing wrong? 

 

 

Here is my script include: 

var zoomLargeMeetingRequestDateValidation = Class.create();
zoomLargeMeetingRequestDateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {

dataValidation: function() {
        var selectedDateParam = this.getParameter('sysparm_date');
        var selectedDate = new GlideDateTime(selectedDateParam);
        var selectedDayOfWeek = selectedDate.getDayOfWeek(); // 1 = Sunday, 7 = Saturday

        // Disallow weekends
        if (selectedDayOfWeek == 1 || selectedDayOfWeek == 7) {
            // gs.info("Selected date cannot be on a weekend.");
            return false;
        }

        // Business schedule (7-5 weekdays)
        var businessSchedule = new GlideSchedule('7f6ae2591bb439d0a3fb62cae54bcb5b'); //7-5 M-F 
        businessSchedule.load();

        // Add 2 business days (20 hours = 2 x 10 working hours)
        var requiredLeadTime = new GlideDuration(20 * 60 * 60 * 1000);
        var currentDate = new GlideDateTime();
        var minAllowedDate = businessSchedule.add(currentDate, requiredLeadTime);

        // Ensure selected date is at least 2 business days in the future
        if (selectedDate.before(minAllowedDate)) {
            // gs.info("Selected date must be at least 2 business days in advance.");
            return false;
        }

        return true;
    },











    type: 'zoomLargeMeetingRequestDateValidation'
});

 

Here's my onChange client script: 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var ga = new GlideAjax('zoomLargeMeetingRequestDateValidation');
    ga.addParam('sysparm_name', 'dataValidation');
    ga.addParam('sysparm_date', newValue);
    ga.getXMLAnswer(function(response) {
        if (response != true) {
            //g_form.showFieldMsg('requested_activation_date', 'Invalid date selected. Please choose a weekday at least 2 business days in advance.', 'error');
            g_form.setValue('requested_activation_date', ''); // Clear invalid value
			g_form.addErrorMessage("Invalid date selected. Please choose a weekday at least 2 business days in advance.");
        }
    });
}
1 ACCEPTED SOLUTION

Try this.  I don't think GlideSchedule will load a cmn_schedule record when you pass a sys_id into the constructor.

        // Business schedule (7-5 weekdays)
        var businessSchedule = new GlideSchedule(); //7-5 M-F 
        businessSchedule.load('7f6ae2591bb439d0a3fb62cae54bcb5b');

 

View solution in original post

6 REPLIES 6

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @0890KM -  At a glance, I think you're actually blocking Mondays and Sundays (Monday = 1, Sunday = 7), not weekends. Also, in your client script, response will be "true" or "false", never the literal boolean. So "true" != true evaluates to true, meaning it always goes into the error path.

 

I haven't done any testing, but if this doesn't fix your issue...it should get you closer.

 

// Disallow weekends
if (selectedDayOfWeek == 6 || selectedDayOfWeek == 7) { // 6=Saturday, 7=Sunday
    return false;
}

 

ga.getXMLAnswer(function(response) {
    if (response !== "true") {  // compare against string, not boolean
        g_form.setValue('requested_activation_date', '');
        g_form.addErrorMessage("Invalid date selected. Please choose a weekday at least 2 business days in advance.");
    }
});

 

It still isn't working.  I updated with your suggestions. 

Try this.  I don't think GlideSchedule will load a cmn_schedule record when you pass a sys_id into the constructor.

        // Business schedule (7-5 weekdays)
        var businessSchedule = new GlideSchedule(); //7-5 M-F 
        businessSchedule.load('7f6ae2591bb439d0a3fb62cae54bcb5b');

 

Thank you so much!!!!!!