- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 01:53 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 12:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 01:04 AM - edited 03-24-2025 01:05 AM
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
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.
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 03:32 AM
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;
}
});
}
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'
});
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 !!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 06:34 AM
Hi @SANDEEP28,
I have written above script :
1. onSubmit client script
Can you please help me to correct that.
Regards,
Nivedita