The Zurich release has arrived! Interested in new features and functionalities? Click here for more

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

You’re welcome. One suggestion: consider clearing the error message when a valid date is chosen, otherwise the error will remain even after correction (unless the user closes it). Also, based on the current logic, it looks like weekends may still be allowed if the selected date is at least two business days out.

palanikumar
Giga Sage

Try replacing your if condition in client script as below:

if (response != "true") {
Thank you,
Palani