Date/Time Change Exclusions Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2024 11:25 PM - edited 09-03-2024 11:42 PM
I have a requirement for the Enterprise:
Essentially, we have new Change Management guidelines that say that users cannot submit requests during business hours. Normally, we'd just black out the business hours and be done with it, but there is a requirement for approved exceptions to take place. I haven't built in the actual exception yet: I was planning on adding a checkbox for that exception, and basically just making it so the business rule doesn't run when the checkbox is true.
There's a secondary restriction that is set up so that if a change would span multiple business days, it must also meet specific criteria.
So I've started working on a business rule that's meant to check the date/time and ensure it's not during business hours. I thought I had it, but it doesn't seem to be working at all. Even more strangely, when I added some logging statements, the date/time field does not match the time that I put in for it.
For example: I've put 2024-09-11 02:01:00 PM as the start_date, but the log comes back as startdate 2024-09-10 22:01:00. And that start date log comes back the same way, no matter what time I put back.
Can anyone take a look and see if maybe I'm missing something obvious? The first time I tried, I was trying to convert it to milliseconds, but that didn't seem to help, either. This is where I am now:
(function executeRule(current, previous /*null when async*/) {
// Defining the business hours between Monday-Saturday, 6am to 10pm cst
var businessStartHour = 6; //6am
var businessEndHour = 22; //10pm
var businessDays = [1, 2, 3, 4, 5, 6];
function isWithinBusinessHours(glideDateTime) {
var dayOfWeek = glideDateTime.getDayOfWeekLocalTime();
var hour = glideDateTime.getHourLocalTime();
return businessDays.includes(dayOfWeek) && (hour >= businessStartHour && hour < businessEndHour);
}
gs.log("vvvv startdate " + current.start_date);
gs.log("vvvv enddate " + current.end_date);
gs.log("vvvv dayOfWeek " + dayOfWeek);
gs.log("vvvv hour " + hour);
//check start_date
if (isWithinBusinessHours(new GlideDateTime(current.start_date))) {
gs.addErrorMessage("Start date/time is within Enterprise Business Hours. Please choose a time outside of business hours, or select an exception.");
current.setAbortAction(true);
return;
}
//check end_date
if (isWithinBusinessHours(new GlideDateTime(current.end_date))) {
gs.addErrorMessage("End date/time is within Enterprise Business Hours. Please choose a time outside of business hours, or select an exception");
current.setAbortAction(true);
return;
}
var startDateTime = new GlideDateTime(current.start_date);
var endDateTime = new GlideDateTime(current.end_date);
var businessDaysCount = 0;
while (startDateTime.getTime() < endDateTime.getTime()) {
if (isWithinBusinessHours(startDateTime)) {
businessDaysCount +=1;
}
startDateTime.addDays(1);
}
current.u_business_days = businessDaysCount.toString();
})(current, previous);
- Labels:
-
Change Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2024 12:09 PM
I've done a few modifications - I've been able to successfully get the date to load through, rather than just being a default value, but I'm still getting stuck on the hour. And the actual exclusions. I hope there may be more help with this new script?
(function executeRule(current, previous /*null when async*/) {
// Defining the business hours between Monday-Saturday, 6am to 10pm cst
var businessStartHour = 6; //6am
var businessEndHour = 22; //10pm
var businessDays = [1, 2, 3, 4, 5, 6];
var dayTime = new GlideDateTime();
var startDateTime = new GlideDateTime(current.start_date);
var dayOfWeek = startDateTime.getDayOfWeekLocalTime();
var dateStr = glideDateTime.getDisplayValue();
var hour = parseInt(dateStr.substring(11, 13), 10);
function isWithinBusinessHours() {
return businessDays.includes(dayOfWeek) && (hour >= businessStartHour && hour < businessEndHour);
}
gs.log("vvvv startdate " + current.start_date);
gs.log("vvvv dayTime " + dayTime.getDisplayValue());
gs.log("vvvv startdisplayvalue " + current.start_date.getDisplayValue());
gs.log("vvvv enddisplayvalue " + current.end_date.getDisplayValue());
gs.log("vvvv startinternalvalue " + current.start_date.getValue());
gs.log("vvvv endinternalvalue " + current.end_date.getValue());
gs.log("vvvv enddate " + current.end_date);
gs.log("vvvv dayOfWeek " + dayOfWeek);
gs.log("vvvv hour " + hour);
//check start_date
if (current.start_date) {
if (isWithinBusinessHours(startDateTime)) {
gs.addErrorMessage("NO");
current.setAbortAction(true);
return;
}
}
//check end_date
if (current.end_date) {
var endDateTime = new GlideDateTime(current.end_date.getDisplayValue());
if (isWithinBusinessHours(endDateTime)) {
gs.addErrorMessage("NO END");
current.setAbortAction(true);
return;
}
}
if (current.start_date.getDisplayValue() && current.end_date.getDisplayValue()) {
var businessDaysCount = 0;
while (startDateTime.getTime() < endDateTime.getTime()) {
if (isWithinBusinessHours(startDateTime)) {
businessDaysCount +=1;
}
startDateTime.addDays(1);
}
current.u_business_days = businessDaysCount.toString();
}
})(current, previous);