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

Yes, this is the code, and yes, this is a reference field. How would I go about referencing the sys_id?

Hi there,

Oke so what is the sys_id for department "accounting"? And if you change the code with that sys_id, does it work then?

if(dept == '347825y8dfsjh23yu8ed_YOUR_SYSID'){

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

I was able to find the sys_id of the department field, but in your example, you have a string of numbers and then _YOUR_SYSID, is there something I need to append to the end of the sys_id?

 

I tried my code with just the sys_id and there was no change

Hi there,

Yes you have to change everything between the quotes for just your sys_id. Can you share your whole code?

Are you sure the UI Policy is triggered at all?

Also, you are hiding/showing fields scripted with the UI Policy. Have you considered showing/hiding these fields just with the UI Policy Actions?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

 

Sure thing, I've added several commands to hide other fields at the begining of the scipt to serve as an indicator that the UI policy is running

 

Here is my full script:

 

function onCondition()
{
// hide all LoB apps
g_form.setVisable('salesforce',False);
g_form.setVisable('factset',False);
g_form.setVisable('dst',False);
g_form.setVisible('pep',False);
g_form.setVisable('fas',False);


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

if(dept == '359330addb90f34025fdd855ca9619d6'){

//show accounting apps
g_form.setVisible('pep',True);
g_form.setVisable('fas',True);

//hide non-accounting apps
g_form.setVisable('salesforce',False);
g_form.setVisable('factset',False);
g_form.setVisable('dst',False);

}

}