Hello @niveditakumari 

 

Please use below on change Client script on your expected date field. Please change the field names with whatever you are using on the form. 

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

 

    var startDate = g_form.getValue('start_date'); // Get the Start Date

    if (!startDate) {

        return;

    }

 

    // Call Script Include to calculate the correct Expected Date

    var ga = new GlideAjax('CalculateBusinessDate');

    ga.addParam('sysparm_name', 'getBusinessDate');

    ga.addParam('sysparm_start_date', startDate);

    ga.addParam('sysparm_user_selected_date', newValue);

 

    ga.getAnswer(function(correctExpectedDate) {

        if (correctExpectedDate && correctExpectedDate !== newValue) {

            g_form.showFieldMsg('expected_date', 'Invalid Expected Date. Adjusted to the next valid business day.', 'error');

            g_form.setValue('expected_date', correctExpectedDate); // Auto-correct the date

 

        }

    });

}

 

Below script include - make sure to check the Client Callable checkbox there. 

 

var CalculateBusinessDate = Class.create();

CalculateBusinessDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    getBusinessDate: function() {

        var startDate = this.getParameter('sysparm_start_date');

        var userSelectedDate = this.getParameter('sysparm_user_selected_date');

        if (!startDate || !userSelectedDate) {

            return '';

        }

 

        var validExpectedDate = this.calculateExpectedDate(startDate); // Get the correct business date

        var userDate = new GlideDateTime(userSelectedDate);

        var userDayOfWeek = userDate.getDayOfWeek(); // 1 = Sunday, ..., 7 = Saturday

 

        // If user-selected date is Friday (6) or Saturday (7), return the correct expected date

        if (userDayOfWeek == 6 || userDayOfWeek == 7 || userDate.getValue() !== validExpectedDate.getValue()) {

            return validExpectedDate.getValue();

        }

 

        return userSelectedDate; // If correct, return the same date

    },

 

    calculateExpectedDate: function(startDate) {

        var businessDays = 7;

        var daysAdded = 0;

        var calculatedDate = new GlideDateTime(startDate); // Start from given Start Date

 

        while (daysAdded < businessDays) {

            calculatedDate.addDays(1);

            var dayOfWeek = calculatedDate.getDayOfWeek(); // 1 = Sunday, ..., 7 = Saturday

 

            if (dayOfWeek != 6 && dayOfWeek != 7) { // Skip Friday (6) & Saturday (7)

                daysAdded++;

            }

        }

 

   

    return calculatedDate;

    }

});

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

niveditakumari
Giga Sage

Hi @Ankur Bawiskar

 

Do I need to do any changes. 

Can you please correct that. 

 

Regards, 

Nivedita 

 

 

@niveditakumari 

do you have a schedule which has that holidays?

If yes then use this link to add 7 days using schedule, compare that date against user given using GlideAjax and then throw error

Add Business Days 

sharing some links which can help you

Variable Date no less than 5 business days 

Auto-populating and validating date fields 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

SANDEEP28
Mega Sage

Hi @niveditakumari 

 

For your requirement you can use OnSubmit catalog client script using GlideAjax and client callable script include.

I have created "Expected Date" field in catalog item for demo. Below client script and script include code is working.

 

You can also use this onChange client script along with onSubmit client script for better user experience. 

 

OnSubmit Client script - 

 

function onSubmit() {

    var validateSelectedDate = new GlideAjax('global.ValidationUtils');
    validateSelectedDate.addParam('sysparm_name', 'validateDate');
    validateSelectedDate.addParam('sysparm_expectedDate', g_form.getValue('expected_date')); //Replace your date time variable
    validateSelectedDate.getXMLAnswer(function getExpectedDate(answer) {
        var validatedOutput = JSON.parse(answer);

        if (validatedOutput.correctDateSelected == 'No') {
            g_form.addErrorMessage('kindly select date after ' + validatedOutput.requiredDate);
            return false;
        }
    });


}

SANDEEP28_0-1742812022106.png

 

Script include - 

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

    validateDate: function() {
       
        var selected_date = new GlideDateTime(this.getParameter('sysparm_expectedDate'));
        var requiredDate = new GlideDateTime(); // Todays Date
        var businessDays = 7;
        var daysAdded = 0;

        while (daysAdded < businessDays) {
            requiredDate.addDays(1); // Move to the next day
            var dayOfWeek = requiredDate.getDayOfWeek(); // 1 = Sunday, 2 = Monday, ..., 7 = Saturday
    
            if (dayOfWeek != 6 && dayOfWeek != 7) { // Exclude Friday (6) and Saturday (7)
                daysAdded++;
            }
        }
		
        var output = {};
        if (selected_date.getDate() > requiredDate.getDate()) {
            output.correctDateSelected = 'Yes';

        } else {
            output.correctDateSelected = 'No';
            output.requiredDate = requiredDate.getDate();
        }

          return JSON.stringify(output);
    },

    type: 'ValidationUtils'
});

SANDEEP28_1-1742812152063.png

 

SANDEEP28_2-1742812290905.png

 

 

As Ankur Said, if you have schedules created then you can check links provided by Ankur,

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

 

 

Hi @SANDEEP28

 

I have written above script :

1. onSubmit client script 

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var validateSelectedDate = new GlideAjax('global.ValidationUtils');
    validateSelectedDate.addParam('sysparm_name', 'validateDate');
    validateSelectedDate.addParam('sysparm_expectedDate', g_form.getValue('expected_date_time')); //Replace your date time variable
    validateSelectedDate.getXMLAnswer(function getExpectedDate(answer) {
        var validatedOutput = JSON.parse(answer);

        if (validatedOutput.correctDateSelected == 'No') {
            g_form.addErrorMessage('kindly select date after ' + validatedOutput.requiredDate);
            return false;
        }
    });
 
2. ScriptInclude 
var ValidationUtils = Class.create();
ValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    validateDate: function() {

        var selected_date = new GlideDateTime(this.getParameter('sysparm_expectedDate'));
        var requiredDate = new GlideDateTime(); // Todays Date
        var businessDays = 7;
        var daysAdded = 0;

        while (daysAdded < businessDays) {
            requiredDate.addDays(1); // Move to the next day
            var dayOfWeek = requiredDate.getDayOfWeek(); // 1 = Sunday, 2 = Monday, ..., 7 = Saturday

            if (dayOfWeek != 6 && dayOfWeek != 7) { // Exclude Friday (6) and Saturday (7)
                daysAdded++;
            }
        }

        var output = {};
        if (selected_date.getDate() > requiredDate.getDate()) {
            output.correctDateSelected = 'Yes';

        } else {
            output.correctDateSelected = 'No';
            output.requiredDate = requiredDate.getDate();
        }

        return JSON.stringify(output);
    },
    type: 'ValidationUtils'
}); 
 
Please find attached screenshot I'm getting error when submitting request : 
Field name : expected_date_time, Date/Time type

niveditakumari_0-1742822925992.png

 

niveditakumari_1-1742823016478.png

 

Can you please help me to correct that. 

 

Regards, 

Nivedita