- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 01:55 AM
Scenario:
- A Change Request is created on Tuesday, April 08, 2025 at 9:00 AM.
- According to our change window schedule this change request is valid since it was created (Tuesday before 5PM), and the planned start date should be (Wednesday after 5PM).
- The original planned start date is correctly set to April 09, 6:00 PM.
Problem: When a user updates the planned start date to Friday, April 11, 2025 at 6:00 PM, our Business Rule re-evaluates the cutoff by dynamically calculating the next Wednesday (April 16) instead of keeping the original cutoff (April 09). This triggers an error: "Start date must be after Wednesday 6:00 PM", even if the planned start date is set in April 11, 2025.
This is the script in Business Rule:
(function executeRule(current, previous /*null when async*/) {
var startDateTime = new GlideDateTime(current.start_date);
var endDateTime = new GlideDateTime(current.end_date);
var createdDateTime = new GlideDateTime(current.sys_created_on);
var schedule1 = new GlideSchedule('sysidofschedulehere'); // Technical Advisory Board Meeting – Monday
var schedule2 = new GlideSchedule('sysidofschedulehere'); // Technical Advisory Board Meeting – Wednesday
var schedule3 = new GlideSchedule('sysidofschedulehere'); // Technical Advisory Board Meeting – Friday
var startDateMonday6pm = getStartImplementMonday6pm();
var startDateWednesday6pm = getStartImplementWednesday6pm();
var startDateFriday6pm = getStartImplementFriday6pm();
// Check if created date and time is within TAB Monday Schedule
if (schedule1.isInSchedule(createdDateTime)) {
// Check if Planned Start Date & Time is before Monday 6:00 PM
if (startDateTime.before(startDateMonday6pm)) {
gs.addErrorMessage('Start date must be after Monday 6:00 PM');
current.setAbortAction(true);
}
} else if (schedule2.isInSchedule(createdDateTime)) {
// Check if Planned Start Date & Time is before Wednesday 6:00 PM
if (startDateTime.before(startDateWednesday6pm)) {
gs.addErrorMessage('Start date must be after Wednesday 6:00 PM');
current.setAbortAction(true);
}
} else if (schedule3.isInSchedule(createdDateTime)) {
// Check if Planned Start Date & Time is before Friday 6:00 PM
if (startDateTime.before(startDateFriday6pm)) {
gs.addErrorMessage('Start date must be after Friday 6:00 PM');
current.setAbortAction(true);
}
}
// Returns the next Monday at 6:00 PM from the current date/time
function getStartImplementMonday6pm() {
var date = new GlideDateTime(); // Get current date/time
var daysToAdd = (8 - date.getDayOfWeek()) % 7 || 7; // Calculate days to next Monday (1 = Monday)
date.addDaysLocalTime(daysToAdd); // Move to next Monday
date.setDisplayValue(date.getDate() + ' 18:00:00'); // Set time to 6:00 PM
return date; // Return the result
}
// Returns the next Wednesday at 6:00 PM from the current date/time
function getStartImplementWednesday6pm() {
var date = new GlideDateTime(); // Get current date/time
var daysToAdd = (10 - date.getDayOfWeek()) % 7 || 7; // Calculate days to next Wednesday (3 = Wednesday)
date.addDaysLocalTime(daysToAdd); // Move to next Wednesday
date.setDisplayValue(date.getDate() + ' 18:00:00'); // Set time to 6:00 PM
return date; // Return the result
}
// Returns the next Friday at 6:00 PM from the current date/time
function getStartImplementFriday6pm() {
var date = new GlideDateTime(); // Get current date/time
var daysToAdd = (12 - date.getDayOfWeek()) % 7 || 7; // Calculate days to next Friday (5 = Friday)
date.addDaysLocalTime(daysToAdd); // Move to next Friday
date.setDisplayValue(date.getDate() + ' 18:00:00'); // Set time to 6:00 PM
return date; // Return the result
}
})(current, previous);
Schedule Context:
Technical Advisory Board Meeting During Monday, Wednesday, Friday. Then implementation date is after TAB Meeting Work hours.
1.) If createdDateTime is between Thursday 5:01pm to Friday 5:00pm. Then planned start date should be Monday 5PM onwards.
2.) If createdDateTime is between Friday 5:01pm - Tuesday 5:00pm. Then planned start date should be Wednesday 5PM onwards.
3.) If createdDateTime is between Tuesday 5:01 pm - Thursday 5:00pm. Then planned start date should be Friday 5PM onwards.
Ideal Scenario:
If the change request is created on Tuesday, April 8 at 8:00 AM, and the planned start date is April 9 at 6:00 PM, then it is valid under our Wednesday schedule. However, if the user updates the planned start date from April 9 to April 11 at 6:00 PM, and the change happens on Thursday, April 10, the system must not recalculate to the next Wednesday schedule (April 16), but should still consider the original Wednesday schedule (April 9). This ensures the planned date can be updated from April 9 onwards.
Solved! Go to Solution.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2025 02:16 AM
try this
(function executeRule(current, previous /*null when async*/) {
var startDateTime = new GlideDateTime(current.start_date);
var createdDateTime = new GlideDateTime(current.sys_created_on);
var startDateWednesday6pm = getStartImplementWednesday6pm(createdDateTime);
// Check if created date and time is within the valid schedule
if (isInValidSchedule(createdDateTime)) {
// Check if Planned Start Date & Time is before the valid start date
if (startDateTime.before(startDateWednesday6pm)) {
gs.addErrorMessage('Start date must be after Wednesday 6:00 PM');
current.setAbortAction(true);
}
}
// Returns the next Wednesday at 6:00 PM from the given date/time
function getStartImplementWednesday6pm(date) {
var daysToAdd = (10 - date.getDayOfWeek()) % 7 || 7; // Calculate days to next Wednesday (3 = Wednesday)
date.addDaysLocalTime(daysToAdd); // Move to next Wednesday
date.setDisplayValue(date.getDate() + ' 18:00:00'); // Set time to 6:00 PM
return date; // Return the result
}
// Check if the created date and time is within the valid schedule
function isInValidSchedule(date) {
var dayOfWeek = date.getDayOfWeek();
var hourOfDay = date.getHourOfDayLocalTime();
var minuteOfHour = date.getMinuteOfHourLocalTime();
// Define the valid schedule ranges
var validRanges = [
{ startDay: 5, startHour: 17, startMinute: 1, endDay: 6, endHour: 17, endMinute: 0 }, // Thursday 5:01 PM to Friday 5:00 PM
{ startDay: 6, startHour: 17, startMinute: 1, endDay: 2, endHour: 17, endMinute: 0 }, // Friday 5:01 PM to Tuesday 5:00 PM
{ startDay: 2, startHour: 17, startMinute: 1, endDay: 4, endHour: 17, endMinute: 0 } // Tuesday 5:01 PM to Thursday 5:00 PM
];
// Check if the date falls within any of the valid ranges
for (var i = 0; i < validRanges.length; i++) {
var range = validRanges[i];
if ((dayOfWeek > range.startDay || (dayOfWeek === range.startDay && (hourOfDay > range.startHour || (hourOfDay === range.startHour && minuteOfHour >= range.startMinute)))) &&
(dayOfWeek < range.endDay || (dayOfWeek === range.endDay && (hourOfDay < range.endHour || (hourOfDay === range.endHour && minuteOfHour <= range.endMinute))))) {
return true;
}
}
return false;
}
})(current, previous);
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
04-10-2025 02:16 AM
try this
(function executeRule(current, previous /*null when async*/) {
var startDateTime = new GlideDateTime(current.start_date);
var createdDateTime = new GlideDateTime(current.sys_created_on);
var startDateWednesday6pm = getStartImplementWednesday6pm(createdDateTime);
// Check if created date and time is within the valid schedule
if (isInValidSchedule(createdDateTime)) {
// Check if Planned Start Date & Time is before the valid start date
if (startDateTime.before(startDateWednesday6pm)) {
gs.addErrorMessage('Start date must be after Wednesday 6:00 PM');
current.setAbortAction(true);
}
}
// Returns the next Wednesday at 6:00 PM from the given date/time
function getStartImplementWednesday6pm(date) {
var daysToAdd = (10 - date.getDayOfWeek()) % 7 || 7; // Calculate days to next Wednesday (3 = Wednesday)
date.addDaysLocalTime(daysToAdd); // Move to next Wednesday
date.setDisplayValue(date.getDate() + ' 18:00:00'); // Set time to 6:00 PM
return date; // Return the result
}
// Check if the created date and time is within the valid schedule
function isInValidSchedule(date) {
var dayOfWeek = date.getDayOfWeek();
var hourOfDay = date.getHourOfDayLocalTime();
var minuteOfHour = date.getMinuteOfHourLocalTime();
// Define the valid schedule ranges
var validRanges = [
{ startDay: 5, startHour: 17, startMinute: 1, endDay: 6, endHour: 17, endMinute: 0 }, // Thursday 5:01 PM to Friday 5:00 PM
{ startDay: 6, startHour: 17, startMinute: 1, endDay: 2, endHour: 17, endMinute: 0 }, // Friday 5:01 PM to Tuesday 5:00 PM
{ startDay: 2, startHour: 17, startMinute: 1, endDay: 4, endHour: 17, endMinute: 0 } // Tuesday 5:01 PM to Thursday 5:00 PM
];
// Check if the date falls within any of the valid ranges
for (var i = 0; i < validRanges.length; i++) {
var range = validRanges[i];
if ((dayOfWeek > range.startDay || (dayOfWeek === range.startDay && (hourOfDay > range.startHour || (hourOfDay === range.startHour && minuteOfHour >= range.startMinute)))) &&
(dayOfWeek < range.endDay || (dayOfWeek === range.endDay && (hourOfDay < range.endHour || (hourOfDay === range.endHour && minuteOfHour <= range.endMinute))))) {
return true;
}
}
return false;
}
})(current, previous);
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