- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 05:26 AM
Hi,
I have a requirement where the date field should not allow user to select next 15 business days. I want to use only Server Side code. Please don't post the links. Kindly help.
The schedule sys_ID from the schedules is 090eecae0a0a0b260077a9dfa71db2e3
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 09:12 PM
This script validates whether the date in a record’s date_field is at least 15 business days in the future. It uses a predefined schedule (identified by schId) to calculate 15 business days from the current date, considering working hours and excluding weekends or holidays. If the provided date is earlier than this calculated threshold, an error message is displayed, and the save or update action is aborted. This ensures that the chosen date aligns with the organization’s business day requirements.
(function executeRule(current, previous) {
// Define the schedule ID. This points to a specific schedule in ServiceNow.
var schId = "090eecae0a0a0b260077a9dfa71db2e3";
// Create a GlideSchedule object using the schedule ID to work with business days.
var sch = new GlideSchedule(schId);
// Get the current date and time.
var now = new GlideDateTime();
// Add 15 business days to the current date based on the schedule.
var next15Days = sch.add(now, 15, "business");
// Extract only the date part (ignoring the time) from the calculated date.
var dateThreshold = new GlideDateTime(next15Days).getDate();
// Check if the date in the 'date_field' is earlier than the threshold date.
if (current.date_field < dateThreshold) {
// If it is, add an error message to the UI.
gs.addErrorMessage("Date must be after 15 business days.");
// Prevent the record from being saved or updated.
current.setAbortAction(true);
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 08:39 AM - edited 01-10-2025 08:40 AM
You can write a before BR with Insert and update selected.
(function executeRule(current, previous /*null when async*/) {
// Get the date selected by the user
var selectedDate = current.your_date_field.getDisplayValue(); // Replace 'your_date_field' with the actual field name
if (!selectedDate) {
return; // No date selected, skip validation
}
// Convert the selected date to a GlideDateTime object
var selectedDateGDT = new GlideDateTime(selectedDate);
var currentDateGDT = new GlideDateTime(); // Today's date
// Calculate the next 15 business days
var nextBusinessDayCount = 0;
var nextBusinessDayGDT = new GlideDateTime(currentDateGDT);
while (nextBusinessDayCount < 15) {
// Add one day
nextBusinessDayGDT.addDaysLocalTime(1);
// Check if it's a weekday
var dayOfWeek = nextBusinessDayGDT.getDayOfWeek(); // Sunday = 1, Saturday = 7
if (dayOfWeek !== 1 && dayOfWeek !== 7) {
nextBusinessDayCount++; // Count only weekdays
}
}
// If the selected date is within the next 15 business days, reject it
if (selectedDateGDT < nextBusinessDayGDT) {
gs.addErrorMessage("You cannot select a date within the next 15 business days.");
current.setAbortAction(true); // Prevent the record from being saved
}
})(current, previous);
Please like/mark my response if it is inline with your expectations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2025 12:45 AM
Hello @Dave_p
You can get some guidance from the below code.
var startTime = new GlideDateTime();
gs.info('Start Time: ' + startTime);
var endTime = new GlideDateTime();
endTime.addDaysUTC(5);
gs.info('No schedule: ' + endTime);
var dc = new DurationCalculator();
var scheduleId = '38fa64edc0a8016400f4a5724b0434b8';
dc.setSchedule(scheduleId);
var seconds = dc.calcScheduleDuration( startTime, endTime);
gs.info('Days in the schedule: ' + Math.floor(seconds / 86400));
Output:
*** Script: Start Time: 2025-01-11 07:42:53
*** Script: No schedule: 2025-01-16 07:42:53
*** Script: Days in the schedule: 3
Even I am adding 5 days, but the schedule is configured to check only week days and hence the output is only 3 days.
If it greater than 15, abort the action.
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2025 12:57 AM
what did you start with and where are you stuck?
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
01-11-2025 04:38 AM
The following Business Rule restricts users from selecting a date within the next 15 business days. It uses the GlideSchedule API to calculate business days based on the provided schedule's sys_id. The script aborts the transaction if the condition is not met.
(function executeRule(current, previous) {
var schId = "090eecae0a0a0b260077a9dfa71db2e3";
var sch = new GlideSchedule(schId);
var now = new GlideDateTime();
var next15Days = sch.add(now, 15, "business");
var dateThreshold = new GlideDateTime(next15Days).getDate();
if (current.date_field < dateThreshold) {
gs.addErrorMessage("Date must be after 15 business days.");
current.setAbortAction(true);
}
})(current, previous);