Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to hide fields based on time

SusanSchwar
Tera Contributor

I have a user who wants a yes or no field hidden at certain times on a servuce catalog request item. Between 8 am and 5 pm the field should be hidden. I know I need to use an onload client script for this, but I can't seem to find the code to make it work. Any suggestions are appreciated. 

1 ACCEPTED SOLUTION

BrianProvencher
Giga Guru

function onLoad() {
// Get the current time
var now = new Date();
var currentHour = now.getHours(); // returns 0-23

// Define the field name you want to show/hide
var fieldName = 'u_custom_field'; // Replace with your actual field name
// Hide the field between 8 AM and 5 PM (inclusive of 8, exclusive of 17)
if (currentHour >= 8 && currentHour < 17) {
gForm.setDisplay(fieldName, false);
} else {
gForm.setDisplay(fieldName, true);
}
}

View solution in original post

4 REPLIES 4

BrianProvencher
Giga Guru

function onLoad() {
// Get the current time
var now = new Date();
var currentHour = now.getHours(); // returns 0-23

// Define the field name you want to show/hide
var fieldName = 'u_custom_field'; // Replace with your actual field name
// Hide the field between 8 AM and 5 PM (inclusive of 8, exclusive of 17)
if (currentHour >= 8 && currentHour < 17) {
gForm.setDisplay(fieldName, false);
} else {
gForm.setDisplay(fieldName, true);
}
}

@BrianProvencher it looks good 👍

@SusanSchwar just check on your global property whether your instance is 12 or 24 format and pray for being it 24 😄 as it would be easier to achieve..

GlideFather_0-1753992275786.png



EDIT: correction prey > pray

_____
This reply is 100 % GlideFather and 0 % AI

Thank you!

Thank you, this was helpful. Ultimately, I needed a server-side script included in my client script to get the time. Here is the script for anyone this may help.

function onLoad() {

    var today = new Date();

    var today_datetime_str = formatDate(today, g_user_date_time_format);

    var time = today_datetime_str.split(' ');
    var hour = time[1].toString().split(':');

    if (parseInt(hour[0].toString()) >= 7 && parseInt(hour[0].toString()) <= 15) {
        g_form.setMandatory('is_the_request_urgent', false);
        g_form.setVisible('is_the_request_urgent', false);
    } else {

        g_form.setVisible('is_the_request_urgent', true);
    }



    /*

   if(GlideTime >= '07:00:00' || GlideTime <= '15:00:00'){

    g_form.setVisible('is_the_request_urgent', false);
   
    }else{

        g_form.setVisible('is_the_request_urgent', true);


    }
    */


}