Extract Time from Date/Time to restrict Change Requests
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi Everyone, I have a strange request from our CAB team, they would like to restricted the ability in a Change request to move to the next step, if the Scheduled time (start_date) is not within the approved window.
My first approach was to just use the OOTB Maintenance Schedule, but this doesn't restrict the change from moving forward it just creates a conflict. I followed up by creating a BR to restrict the change, if a conflict is detected, but of course, that won't work as other conflict happen that shouldn't be stopping the process.
My final step was to create an Approved Maintenance Filed (u_approved_maintenance_start) and a matching (u_approved_maintenance_end), where the CAB team can assigned the correct time's for a maintenance period. This is currently populated with in the Scope field of the planning tab as a text, but I didn't want to part out the time from this field.
The creation when smooth, and my below code will correctly block the progression from one stage to the next, but it blocks it regardless of what time is entered within the fields. It will allow the change to move to the next step if no time is in the Maintenance field, but soon as one is entered, and I save the request, the error banner correctly pops.
I'm assuming that my code below isn't correctly pulling the time. In the test case I'm using a change is being requested, but the maintenance window is only 9 pm - 5 am daily. Any time outside that window should be rejected with an error. In production we have other times, like not until after 8am, or only on Weekends, which is why I'm attempting it this way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago - last edited 2 hours ago
Your logic is returning time in UTC:
// Function to extract time from GlideDateTime as a string in "HH:mm:ss" format
function getTimeOnly(dateTime) {
gs.info('getting time from: ' + dateTime);
return dateTime.getDisplayValue().split(' ')[1];
}
// Retrieve and extract time from the scheduled date/time fields
// var scheduledStartTime = getTimeOnly(current.start_date);
// var scheduledEndTime = getTimeOnly(current.end_date);
var sdt = new GlideDateTime("2025-05-22 06:30:00");
var edt = new GlideDateTime("2025-05-22 08:30:00");
var st = getTimeOnly(sdt);
var et = getTimeOnly(edt);
gs.info('start: ' + st);
gs.info('end: ' + et);
To avoid the above, you may want to create an "onSubmit" client script for the change_request table with similar logic used by the "Change conflict" client script that runs on the change_request. The onSubmit client script can abort the update to the record.