- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2025 08:55 PM - edited ‎03-12-2025 09:23 PM
In Change Management, we want to restrict a date & time field named 'start_date' so that users can only select dates after a specific date and time.
For example:
- If a user submits a change request on Thursday or Friday, the 'start_date' variable should only allow selection from next Tuesday at 5:00 PM onwards. All dates from the current date (Thursday/Friday) up to Tuesday at 5:00 PM should be restricted. However, if a user selects a Monday or Tuesday in the following weeks or a month from the current date, they should be able to proceed.
- If a user submits a change request on Monday or Tuesday, the 'start_date' variable should only allow selection from Thursday at 5:00 PM onwards. All dates from the current date (Monday/Tuesday) up to Thursday 5:00 PM should be restricted. However, if a user selects a Wednesday or Thursday in the following weeks or a month from the current date, they should be able to proceed.
Any idea how the restriction could be implemented in a Business Rule? We already know how the scheduling works, just the restrictions.
if FridayDeadline.isInSchedule(CreatedDateTime){
if start_date is before next Tuesday 5:00pm
then error, abort
else
success
}
Solved! Go to Solution.
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-12-2025 09:15 PM
try this in before update business rule
Create Script Include:to calculate the next Tuesday at 5:00 PM from a given date:
var DateUtils = Class.create();
DateUtils.prototype = {
initialize: function() {},
getNextTuesdayAt5PM: function(currentDate) {
var date = new GlideDateTime(currentDate);
var dayOfWeek = date.getDayOfWeek(); // 1 = Sunday, 7 = Saturday
// Calculate days to add to reach next Tuesday
var daysToAdd = (9 - dayOfWeek) % 7; // 9 because Tuesday is 3rd day of the week and we need to add 7 days if it's already Tuesday
date.addDaysLocalTime(daysToAdd);
date.setDisplayValue(date.getDisplayValue().split(' ')[0] + ' 17:00:00'); // Set time to 5:00 PM
return date;
},
type: 'DateUtils'
};
business rule: Condition: Your date/time field changes
(function executeRule(current, previous /*null when async*/) {
var dateUtils = new DateUtils();
var nextTuesdayAt5PM = dateUtils.getNextTuesdayAt5PM(new GlideDateTime());
var startDate = new GlideDateTime(current.start_date);
if (startDate.before(nextTuesdayAt5PM)) {
gs.addErrorMessage('The start date must be after next Tuesday at 5:00 PM.');
current.setAbortAction(true);
}
})(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
‎03-12-2025 09:15 PM
try this in before update business rule
Create Script Include:to calculate the next Tuesday at 5:00 PM from a given date:
var DateUtils = Class.create();
DateUtils.prototype = {
initialize: function() {},
getNextTuesdayAt5PM: function(currentDate) {
var date = new GlideDateTime(currentDate);
var dayOfWeek = date.getDayOfWeek(); // 1 = Sunday, 7 = Saturday
// Calculate days to add to reach next Tuesday
var daysToAdd = (9 - dayOfWeek) % 7; // 9 because Tuesday is 3rd day of the week and we need to add 7 days if it's already Tuesday
date.addDaysLocalTime(daysToAdd);
date.setDisplayValue(date.getDisplayValue().split(' ')[0] + ' 17:00:00'); // Set time to 5:00 PM
return date;
},
type: 'DateUtils'
};
business rule: Condition: Your date/time field changes
(function executeRule(current, previous /*null when async*/) {
var dateUtils = new DateUtils();
var nextTuesdayAt5PM = dateUtils.getNextTuesdayAt5PM(new GlideDateTime());
var startDate = new GlideDateTime(current.start_date);
if (startDate.before(nextTuesdayAt5PM)) {
gs.addErrorMessage('The start date must be after next Tuesday at 5:00 PM.');
current.setAbortAction(true);
}
})(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