UI Policy onCondition Script

bradfournier
Kilo Expert

I'm having trouble configuring when a field is mandatory and when it is not using a UI Policy script. I have a field u_request_type on the form that the UI policy condition depends on. The UI policy should go into effect when certain options are selected in the u_request_type field. When the conditions are met, the UI policy should make the u_site_type field visible. The tricky part is that I need the u_site_type field to be mandatory for some options, but not mandatory for others. Below is the scripts I have:

Execute if true:

function onCondition() {

  if (g_form.getValue('u_request_type') == 'Admin'){

        g_form.setDisplay('u_site_type', true);

  }

  else if ((g_form.getValue('u_request type') == 'Access/Permissions') || (g_form.getValue('u_request_type') == 'How-To')){

        g_form.setDisplay('u_site_type', true);

        g_form.setMandatory('u_site_type', true);

  }

}

Execute if false:

function onCondition() {

  g_form.setDisplay('u_site_type', false);

}

The UI Policy condition is set to run whenever the u_request_type is one of Admin, Access/Permission, or How-To. So if any option other than one of those three is selected, the u_site_type field should not be visible at all. I should note that I have already tried getting it working using two separate normal UI policies: one for the mandatory options and one for the non-mandatory option, however they seemed to end up conflicting with each other. Any help here is greatly appreciated!

10 REPLIES 10

Thanks. I don't see anything offhand that gives me concern. When scripts aren't working as expected, I start throwing in debug statements. It might be time for some g_form.addInfoMessage() statements. With a UI policy like this, have you considered using a client script instead?



Reference:GlideForm (g form) - ServiceNow Wiki


Client Scripts - ServiceNow Wiki


veena_kvkk88
Mega Guru

Hi Bradley,



From the looks of it, there doesn't seem to be anything wrong. Can you elaborate on what is happening when you change the choices for Request Type? Any particular error or is it behaving randomly?


Also, there's a chance there are other UI policies working with the same fields. That's what happened when I faced the issue. I had to change existing UI policies as well to incorporate the new requirements, since they were handling the same fields.


Hi,



So far in testing, the behavior seems to be completely random. Sometimes the u_site_type field shows up, sometimes it doesn't. Sometimes it is mandatory, sometimes not. I have also checked through the other UI policies on that table to make sure none of the others using the same fields.



I am going to start trying some g_form.addInfoMessage() statements to the script and see if that helps me at all.


captainpugwash
Kilo Expert

Hi Bradley,



Did you find what you were looking for?



I'd like a simpler policy where worknotes are mandatory on change of assignment group except if assignment group is empty.



The assignment group change is working but also on first assignment from empty



Ben,