Help Needed: Restricting Planned Start Time in Change Module (CST Timezone)

suvarnamoha
Tera Contributor

Hi All,

I have a requirement in the Change module to restrict the Planned Start Date field so that users are not able to select a time between 9:00 AM to 6:00 PM CST.

This restriction should be enforced based on the CST timezone, regardless of the user's local time setting.

Any guidance or support on how to implement this would be greatly appreciated.

Thanks in advance!

 

Regards,

SM_L

5 REPLIES 5

pratikjagtap
Giga Guru

Hi @suvarnamoha ,

 

Try Script include and client script :

1. Script Include (for UTC ➝ CST conversion):

// Name: TimezoneUtils
// Accessible from: All application scopes
var TimezoneUtils = Class.create();
TimezoneUtils.prototype = {
initialize: function() {},

convertToCST: function(utcDateTimeStr) {
var gdt = new GlideDateTime(utcDateTimeStr);
gdt.setTZ('America/Chicago'); // CST/CDT depending on time of year
return gdt.getDisplayValue();
},

type: 'TimezoneUtils'
};

 

2. Client Script (Type: onChange or onSubmit):

// Type: onChange (field: planned_start_date) OR onSubmit
(function executeRule(current, gForm, gUser, gSNC) {

function checkCSTTime(plannedDate) {
var ga = new GlideAjax('TimezoneUtils');
ga.addParam('sysparm_name', 'convertToCST');
ga.addParam('sysparm_utcDateTimeStr', plannedDate);
ga.getXMLAnswer(function(cstDateTimeStr) {
var planned = new GlideDateTime(cstDateTimeStr);
var timeOnly = planned.getTime().getByFormat('HHmm'); // 'HHmm' format

var timeInt = parseInt(timeOnly, 10);
if (timeInt >= 900 && timeInt <= 1800) {
gForm.showFieldMsg('planned_start_date', 'Time must be outside 9:00 AM to 6:00 PM CST', 'error');
gForm.setValue('planned_start_date', '');
}
});
}

var plannedStart = gForm.getValue('planned_start_date');
if (plannedStart) {
checkCSTTime(plannedStart);
}

})(current, gForm, gUser, gSNC);

 

If my response helped, please hit the 👍Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Pratik

 

Thank you for your response. I tried the solution you suggested, but it’s not working as expected. The client script seems to halt execution at the line:

 

var planned = new GlideDateTime(cstDateTimeStr);

After this line, the script doesn’t continue, and the record gets created without any further execution of the client script.

Ankur Bawiskar
Tera Patron
Tera Patron

@suvarnamoha 

try this in before insert/update BR with condition as

Planned Start Date changes

(function executeRule(current, previous /*null when async*/) {
    var gdt = new GlideDateTime(current.planned_start_date);
    gdt.setTZ('America/Chicago'); // CST timezone

    var hour = gdt.getHourLocalTime();
    if (hour >= 9 && hour < 18) {
        gs.addErrorMessage('Planned Start Date cannot be between 9:00 AM and 6:00 PM CST.');
        current.setAbortAction(true);
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@suvarnamoha 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader