I need to allow user only to select date after 7 days excluding business days.

niveditakumari
Mega Sage

Hi,  

 

I need to allow user to select date after 7 days but it should calculate only business days not weekend. In our project as per client we follow Friday, Saturday as weekend and I need to exclude Friday, Saturday. When user select date then it should calculate for Sunday, Monday, Tuesday, Wednesday, Thursday as business day.  

Can you please help me with that. 

 

Regards, 

Nivedita 

 

 

1 ACCEPTED SOLUTION

@niveditakumari 

it worked see

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().toString();
        }
        return JSON.stringify(output);
    },
    type: 'ValidationUtils'
}); 

AnkurBawiskar_1-1742903835567.png

 

Client Script:

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

    g_form.hideFieldMsg('my_datetime');

    var validateSelectedDate = new GlideAjax('global.ValidationUtils');
    validateSelectedDate.addParam('sysparm_name', 'validateDate');
    validateSelectedDate.addParam('sysparm_expectedDate', g_form.getValue('my_datetime')); //Replace your date time variable
    validateSelectedDate.getXMLAnswer(function getExpectedDate(answer) {
        var validatedOutput = JSON.parse(answer);
        if (validatedOutput.correctDateSelected == 'No') {
            g_form.showFieldMsg('my_datetime', 'kindly select date after ' + validatedOutput.requiredDate.toString(), 'error');
        }
    });

    //Type appropriate comment here, and begin script below

}

AnkurBawiskar_0-1742903809426.png

 

Output:

business days.gif

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

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

View solution in original post

38 REPLIES 38

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 

 

 

@niveditakumari 

update as this to show that date

g_form.addErrorMessage('kindly select date after ' + validatedOutput.requiredDate.toString());

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

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

@niveditakumari  You only need to change below line of code

 

 g_form.addErrorMessage('kindly select date after ' + validatedOutput.requiredDate.toString());

 

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')); //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.toString());
            return false;
        }
    });


}

 

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

Hi @SANDEEP28

 

I have made that changes. 

I'm getting still same error. 

 

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.toString());
            return false;
        }
    });
 

niveditakumari_0-1742828634991.png  

 

Can you please help me to correct that. 

 

Regards, 

NIvedita 

 

 

 

Hello @niveditakumari 

 

Did you try my script ? 

 

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