Hide fields in user table using client script

Govind Bysani
Tera Contributor

In my user table, I have created a section called 'Additional details', In that section I have 5 fields in it, I need to hide 2 of those 5 fields using client script. Can anyone help me on this.

3 REPLIES 3

Astik Thombare
Tera Sage

Hi @Govind Bysani ,

 

To conditionally hide fields in a form, choose the appropriate client script type (e.g., onload, onchange) and use the g_form.setDisplay('field_name', false) method within the script. This allows you to customize field visibility based on your specific requirements.

 

Please let me know if this helps! You can mark this answer as "Correct" if it resolves your issue, and "Helpful" if you find it valuable.

 

Thanks,

Astik Thombare

Sandeep Rajput
Tera Patron
Tera Patron

@Govind Bysani 

setDisplay(String fieldName, Boolean display)

Displays or hides a field.

This method cannot hide a mandatory field with no value. If the field is hidden, the space is used to display other items. Whenever possible, use a UI policy instead of this method.

 

Here is an example 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   //If the page isn't loading
   if (!isLoading) {
      //If the new value isn't blank
      if (newValue != '') {
         g_form.setDisplay('priority', false);   
      }
      else 
         g_form.setDisplay('priority', true);
      }
   }

 

Here is the API reference https://developer.servicenow.com/dev.do#!/reference/api/vancouver/client/c_GlideFormAPI#r_GlideFormS...

 

Hope this helps.

Gopal Allu
Tera Expert

Hi @Govind Bysani ,

 

You can create one onload , onchange client scripts on this user table like shown in below.

 

table : sys_user

 

condition based you want hide and show?

 

for example I am providing the condition as while onchange on checkbox, script. You can hide the fields according to the checkbox value is true/false those fields can be shown,

 

script:

var a = g_form.getValue('u_checkbox_test');
    if (a == 'false') {
        g_form.setDisplay('u_field_1', false);
        g_form.setDisplay('u_field_2', false);
    } else {
        g_form.setDisplay('u_field_1', true);
        g_form.setDisplay('u_field_2', true);
    }
 
Usage of script in two client scripts giving in images:
GopalAllu_0-1712070704043.pngGopalAllu_1-1712070741963.png

 

If it is helpful please make it correct/helpful.

 

Thanks and Regards,

Allu Gopal.