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

So maybe still some typos in the UI Policy script?

Also again, have you considered making the fields visible / hide the fields thru the UI Policy Actions? These just work with some clicks in the UI, rather than scripting.

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 would prefer to use the IU policy action, but I run into a problem when a field is referenced un multiple IP Policies, therefore a IU Script is needed. Would this be better served as a client script?

Ah clear, you gave it a well thought, so that's fine. UI Policy / Client Script, both are possible.

In this case, actually, are any fields hidden according to your script? My guess is, the if statement won't work at all!

So the first fields under "// hide all LoB apps" - apart of the typos - will work? But the ones in the if statement won't work. Is my guess right?
That's where you could use the UI Policy condition for.

You are trying to combine two actions now in one. While having no condition on the UI Policy, will make this only run on load, not on change of your value. Setting up an additional UI Policy would do the trick.

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 there,

I wanted to simulate your situation, though my eyed catched the UI Policy. Is this a UI Policy or a Catalog UI Policy? Reason for asking, I can't clearly see this in your screenshot. Looking at your screenshot though, a (custom) table name is mentioned. This would suggest that this is a UI Policy on a form in the Platform UI, not a Catalog Item on the Service Portal.
Therefore, unfortunately, I could not simulate your situation.

A custom table is mentioned (u_), while looking at your script, fields like "salesforce", "dst", "pep", "fas" are mentioned. These don't have the u_ prefix. Is this correct? Shouldn't these be custom fields? Or where do these come from?

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

Good Morning Mark,

 

Thank you for your continued ideas.

 

So I believe that this is a UI Policy and not a UI Catalog Policy, how can I confirm?

 

Yes, this is an extension of the request table. I had also thought I wasn’t referencing the fields properly and tried both u_fieldvalue and fieldvalue, but neither made a difference.

 

I think you may have been on to something when you suggested adding the line

 

g_form.addInfoMessage(g_form.getValue('department'));

 

When I add that line in a Client Script I receive the banner at the top of the form, but nothing seems to run when I do this for a UI policy.  Is starting the script with Function onCondition(); correct? I feel that I don’t have the correct conditions to trigger the script.

 

Thanks!

--Troy