Auto populate a date after 2 business days from current date in catalog form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 10:32 AM
I have a date field in the catalog form. I want the date field to be auto-populated to a date after 2 business days from current date. The script should only consider week days. It should throw error when user tries to odify the date less than 2 business days from current date. I tried onchange client script and script include, also script in default value from community, but nothing worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 10:39 AM
Hi there,
Can you share what you tried and is not working?
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 11:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2025 01:57 AM
Hi Sireesha,
I have tried to using schedule for excluding weekends and holidays to auto populate the date field after 7 days based on current date .but couldn't work.
Please let me know if it is worked fine ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2024 10:44 AM
Hi @sireesha09,
Step 1: Client Script :
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var selectedDate = new GlideDateTime(newValue);
var currentDate = new GlideDateTime();
// Calculate the date after 2 business days
var businessDays = 0;
var daysToAdd = 2; // Change this value if you want more than 2 business days
while (businessDays < daysToAdd) {
selectedDate.addDays(1);
if (!isWeekend(selectedDate.getDayOfWeek())) {
businessDays++;
}
}
// Set the calculated date back to the field
g_form.setValue(control.name, selectedDate.getValue());
// Validation: Prevent selection of dates less than 2 business days from current date
var minDate = new GlideDateTime();
minDate.addDays(daysToAdd);
if (selectedDate.compareTo(minDate) < 0) {
g_form.clearValue(control.name);
g_form.showErrorBox(control.name, "Please select a date at least 2 business days from today.");
}
}
function isWeekend(dayOfWeek) {
// 1 = Sunday, 7 = Saturday
return dayOfWeek != 1 && dayOfWeek != 7;
}
Step 2: Business Rule:
(function executeRule(current, previous /*null when async*/) {
var currentDate = new GlideDateTime();
var businessDays = 0;
var daysToAdd = 2;
while (businessDays < daysToAdd) {
currentDate.addDays(1);
if (!isWeekend(currentDate.getDayOfWeek())) {
businessDays++;
}
}
current.date_field = currentDate;
})(current, previous);
Thank you, please make helpful if you accept the solution.