show/hide fields based on another fields vaule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2019 01:25 PM
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);
}
}
- Labels:
-
Request Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2019 09:01 AM
Hi Troy,
Well finally figured out what's going wrong.
1) As mentioned, you do have to use a second UI Policy, with the department condition. Because this UI Policy has no condition and only runs onLoad. The IF will be ignored.
2) Reason that the fields mentioned with the setVisible are not hiding... False should be lowercase. If didn't notice the uppercase before, just when I copied your code I saw False was not highlighting in the editor.
Also changed the setVisible typos.
See screenshots below.
If my answer helped you in any way, please then mark it as helpful.
Please mark this answer as correct if it solves your problem. This will help others who are looking for a similar solution.
Thanks.
Kind regards,
Mark
---
Your code copy/paste + added the g_form.addErrorMessage to 'debug' if the UI Policy is triggered.
UI Policy triggered.
Changed some values so I could test with my OOB incident form.
Fields are hidden 🙂
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2019 11:41 AM
So I think I'm really close.
I deleted my original UI Policy Script and started fresh. The new script allowed the debug step to run (progress!). From there I wanted to see if I could manipulate the form using the setDisplay or setVisible commands. As it turns out, these command do not want to apply to a True/False field type, but will on a String field Type.
Not sure what to make of this. I am going to continue to play around with this and may need to redesign the form.
Something else I noticed is that it is not recommended to use the setVisible and setDisplay commands on a UI Policy.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2019 07:17 AM
Hmmmmm I see also some errors in your script. Look closely at:
g_form.setVisible
g_form.setVisable
Some typos here.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2019 07:24 AM
whoops - corrected, no change.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2019 11:49 AM
I would put an alert statement in there to be sure it is actually getting called. If your alert confirms that it is, then maybe try true/false versus yes/no. For example:
g_form.setDisplay('priority', false);