Extract Time from Date/Time to restrict Change Requests

cmaleblanc
Tera Contributor

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. 

 

(function executeRule(current, previous /*null when async*/) {

    // Function to extract time from GlideDateTime as a string in "HH:mm:ss" format
    function getTimeOnly(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);

    // Retrieve and extract time from the maintenance window fields
    var maintenanceWindowStartTime = getTimeOnly(current.u_approved_maintenance_start);
    var maintenanceWindowEndTime = getTimeOnly(current.u_approved_maintenance_end);

    // Compare only the time parts
    if (scheduledStartTime < maintenanceWindowStartTime || scheduledEndTime > maintenanceWindowEndTime) {
        // Create an error message
        var message = 'You have selected a Planned window not within the Approved Maintenance Period, Please review the Scope feild and select a time within the approved window';

        // Set an error message
        gs.addErrorMessage(message);

        // Prevent the record from moving to the next step
        current.setAbortAction(true);
        return false;
    }

})(current, previous);
 
If anyone has a suggestion on what I might be doing wrong, or if you think of a method I haven't for making this work that would be great. 
 
Thank you.

 

 

1 REPLY 1

Bert_c1
Kilo Patron

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.