Change which field are visible or hidden based on select box choice

tkh
Kilo Expert

I am fairly new to Service Now, if not entirely new to scripting, so please be patient with this newby.  

We have several Service Catalogs that all share a core set of fields, then each request has a few additional fields that are related to that specific type of change.   This leaves us with a bunch of Service Catalogs that are 90% the same.   What I had thought about doing would be to make a more dynamic form, that could hide or show fields based on the type of request being selected from a Select Box.

Ideally the form would load with only the Select Box showing, then based on which request type they choose from the Select Box would show the required fields.   I have tried using Catalog UI Policy but running into "There are multiple UI Policies with the same Order for this field - their run order is not predictable" errors then the fields work correctly on one policy but not the next policy.

I also tried using scripts but wasn't able to get the OnChange to work when the selection was changed.

So I am looking for some advice as to the best method to accomplish this.   And thank you in advance for your help.

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

Hi Trudy,



It sounds like your issue is about how to structure your UI Policies.



For example, say you have 3 options in your selectbox, "Request type 1", "Request type 2" and "Request type 3". And you have a Field called "Email" that should only be visible for "Request type 1" and "Request type 3".


You probably have 2 UI Policies affecting this field:


UI Policy 1: Show all fields for Request type 1 (which includes Email)


UI Policy 2: Show all fields for Request type 3 (which also includes Email)



Both of these UI Policies have the same Order and you will see that message. The Order field is not shown on the form out-of-the-box, but it defines the execution order of the UI Policies.



I find that the best way to solve this is to have 1 UI Policy per field:


UI Policy 1: Show Email (Condition is Request type is 1 OR 3)


There are smarter and more effiecient ways to do it, but this (IMHO) is the easiest way to understand and maintain. It usually means more UI Policies, but it's worth it to keep the logic simple.



If you need to have 2 UI Policies for a field, because the times it should be visible and different to the times it should be mandatory (eg, for some requests the field is visible but optional), then I would basically have 2 sets of UI Policies.   Ones that show fields at Order 100, and ones that makes fields mandatory at Order 110. But I would only have 1 UI Policy defining when to show a field, and 1 UI Policy defining when to make it mandatory.


View solution in original post

7 REPLIES 7

Steven G_
Tera Expert

Hi Trudy,



Unfortunatly you will need a client script that manages the selection, it will get a bit complex because of the dynamic nature of the input. I would suggest you map this all out and define any static fields you can then leverage logic from. It is difficult to create a script with a dynamic input and have a dynamic output.



Here is a client script that we wrote, if you slect one, it will limit the aviable selection in the next variable:



find_real_file.png


I tried this up still getting same outcome when I make a change nothing happens.   Perhaps I am putting my script in the wrong place?   I was putting it in the Client Catalog scripts.


tkh
Kilo Expert

I would still like to figure out why I can't get the script to work as I think I will need something like this in the near future.   I know it has to be user error, just need to figure out what I am doing wrong.



As always thank you for your time and efforts.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

Hi Trudy,



It sounds like your issue is about how to structure your UI Policies.



For example, say you have 3 options in your selectbox, "Request type 1", "Request type 2" and "Request type 3". And you have a Field called "Email" that should only be visible for "Request type 1" and "Request type 3".


You probably have 2 UI Policies affecting this field:


UI Policy 1: Show all fields for Request type 1 (which includes Email)


UI Policy 2: Show all fields for Request type 3 (which also includes Email)



Both of these UI Policies have the same Order and you will see that message. The Order field is not shown on the form out-of-the-box, but it defines the execution order of the UI Policies.



I find that the best way to solve this is to have 1 UI Policy per field:


UI Policy 1: Show Email (Condition is Request type is 1 OR 3)


There are smarter and more effiecient ways to do it, but this (IMHO) is the easiest way to understand and maintain. It usually means more UI Policies, but it's worth it to keep the logic simple.



If you need to have 2 UI Policies for a field, because the times it should be visible and different to the times it should be mandatory (eg, for some requests the field is visible but optional), then I would basically have 2 sets of UI Policies.   Ones that show fields at Order 100, and ones that makes fields mandatory at Order 110. But I would only have 1 UI Policy defining when to show a field, and 1 UI Policy defining when to make it mandatory.