- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2025 12:37 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2025 04:57 AM
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'
});
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
}
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 01:38 AM
Hi @Shivalika,
I have catalog item and in that I have field ABC when user select date in that field then it should calculate 5business day. Today is 24th March and in my field when user select tomorrow date then it should show field error message that please select date after 5 business days, when user select 29th March then it should show same field error message because Friday, Saturday comes under as weekend, when user select 31st then it should not show field error message because it is completing it's 5business day.
Hope you are clear with my requirement.
Can you confirm what step Do I need to follow to achieve that.
Regards,
Nivedita
- 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 || ✨ 9x 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 !!