How to make Catalog Item variables read-only based on UTC business hours in Employee Center?

Sirri
Kilo Sage

Hi Team,

I have a requirement for a ServiceNow Catalog Item in Employee Center.

The requirement is:

  • During 9:00 AM to 6:00 PM Riyadh time, users should not be allowed to submit the Catalog Item.
  • This is equivalent to 6:00 AM to 3:00 PM UTC.
  • During this time window, I want to make the Catalog Item variables read-only and prevent users from submitting the request.
  • Outside this time window, the variables should remain editable and users should be able to submit the request.

    How I can achieve this requirement Please someone provide the entire code that will help full to me.

    Below are fields only available in that catalog item & I need to showcase error message:
      g_form.setReadOnly('requester', true);
                g_form.setReadOnly('requested_for', true);
                g_form.setReadOnly('justification', true);
                g_form.addErrorMessage('This request cannot be submitted between 9:00 AM and 6:00 PM Riyadh time.');


    Thank you.
1 REPLY 1

Rafael Batistot
Mega Patron

Hi @Sirri 

 

To get some variables read only use onLoad

 

function onLoad() {
var now = new Date();

var utcHour = now.getUTCHours();
var utcMinute = now.getUTCMinutes();

var currentTime = (utcHour * 60) + utcMinute;

var startTime = 6 * 60;
var endTime = 15 * 60;

var isRestricted = currentTime >= startTime && currentTime < endTime;

if (isRestricted) {
g_form.setReadOnly('requester', true);
g_form.setReadOnly('requested_for', true);
g_form.setReadOnly('justification', true);

g_form.addErrorMessage(
'This request cannot be submitted between 9:00 AM and 6:00 PM Riyadh time.'
);
}
}

 

To prevent hit submit use onSubmit 

 

function onSubmit() {
var now = new Date();

var utcHour = now.getUTCHours();
var utcMinute = now.getUTCMinutes();

var currentTime = (utcHour * 60) + utcMinute;

var startTime = 6 * 60;
var endTime = 15 * 60;

var isRestricted = currentTime >= startTime && currentTime < endTime;

if (isRestricted) {
g_form.addErrorMessage(
'This request cannot be submitted between 9:00 AM and 6:00 PM Riyadh time.'
);

return false;
}

return true;
}

***Note: 

 

JavaScript running in the browser uses the user’s local timezone, so you should explicitly calculate the time in UTC to avoid timezone inconsistencies.