show/hide fields based on another fields vaule

tbofinger
Tera Contributor

Hi everyone,

 

I’m new to Java scripting, so apologies in advance for the beginner question.

 

I am trying to build a user enablement form that will dynamically update the software list based on the department field.

I’ve created the UI policy script below, and while I’ve stopped getting script errors, the form doesn’t update as expected.

 

Any help would be appreciated.

 

function onCondition()

{

 

                var dept = g_form.getValue('department');

               

                if(dept == 'accounting'){

                               

                                //show accounting apps

                                g_form.setDisplay('pep',Yes);

                                g_form.setDisplay('fas',Yes);

                               

                                //hide non-accounting apps

                                g_form.setDisplay('salesforce',No);

                                g_form.setDisplay('factset',No);

                                g_form.setDisplay('dst',No);

                               

                }

               

}

35 REPLIES 35

williamsun
Mega Guru

Try to use true/false instead of yes/no:

g_form.setDisplay('priority', false);

 

https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_GlideFormSetDisplay_String_Boolean

Prateek kumar
Mega Sage

Is the script not at all working? Where are you stuck exactly?

function onCondition()

{
   var dept = g_form.getValue('department');
  if(dept == 'accounting'){
  //show accounting apps
 g_form.setDisplay('pep',true);
 g_form.setDisplay('fas',true);
//hide non-accounting apps
 g_form.setDisplay('salesforce',false);
g_form.setDisplay('factset',false);
 g_form.setDisplay('dst',false);
  }

}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Hi Prateek, thanks for replying!

 

The issue that I'm having is that when I populate the Department field, Accounting in this example, the form doesn't update to show/hide the fields indicated in the script above.

 

I thought that I may need to change the function call from:

 

function onCondition() 

To:

function onChange(control, oldValue, newValue, isLoading)

 

But when I make that change I receive a"missing function declaration for onCondition" error.

 

Any thoughts or advice is greatly appreciated. 

 

thanks,

Troy

 

 

 

I expect these values should be hidden/visible even on the change of department correct??

If so,you would need an OnChange Client script. Try below:

Replace table, Field name to department

find_real_file.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading|| newValue == '') {
return;
}
var dept = g_form.getValue('department');
if(dept == 'accounting'){
//show accounting apps
g_form.setDisplay('pep',true);
g_form.setDisplay('fas',true);
//hide non-accounting apps
g_form.setDisplay('salesforce',false);
g_form.setDisplay('factset',false);
g_form.setDisplay('dst',false);
}
}


Please mark my response as correct and helpful if it helped solved your question.
-Thanks