- 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-11-2025 05:10 AM
use a business rule with the following server side code:
(function executeRule(current, gS) {
var grSchedule = new GlideSchedule('090eecae0a0a0b260077a9dfa71db2e3');
var targetDate = new GlideDateTime(current.date_field);
if (grSchedule.isInSchedule(targetDate)) {
var next15BusinessDays = new GlideDateTime();
next15BusinessDays.addBusinessDaysLocalTime(15);
if (targetDate <= next15BusinessDays) {
gs.addErrorMessage('Date selection within the next 15 business days is not allowed.');
current.setAbortAction(true);
}
}
})(current, gs);
This will block submission if the date falls within the next 15 business days. Adjust "date_field" to match your field name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 09:22 AM
Hi @yuvarajkate,
I like your approach but, I am having hard time understanding the code. Can you please help me understand it.
(function executeRule(current, previous) {
var schId = "090eecae0a0a0b260077a9dfa71db2e3";
var sch = new GlideSchedule(schId);
var now = new GlideDateTime();
var next15Days = sch.add(now, 15, "business"); //I don't understand this line
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);
Regards
Suman P.
- 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-13-2025 11:14 PM
Hi @yuvarajkate,
I still have issues. After selecting today's date and submitting it, it is still generating a Request. Kindly help.
(function executeRule(current, previous /*null when async*/) {
var sch = '090eecae0a0a0b260077e1dfa71da828';
var sche = new GlideSchedule(sch);
var now = new GlideDateTime();
var nxt15Days = sche.add(now, 15, 'business');
var dateThreshold = new GlideDateTime(nxt15Days).getDate();
if(current.days < dateThreshold){
gs.addErrorMessage('Date must be after 15 business days');
}
current.setAbortAction(true);
})(current, previous);
Regards
Suman P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2025 02:12 AM
You should use Before Business Rule not After.
Before BR will validate before a record is inserted.
There is no use in validating it after the record is inserted.
Hope this helps you.