Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Community Alums
Not applicable

you should execute the g_form methods in the following order

g_form.clearValue('u_configuration_item');
g_form.setMandatory('u_configuration_item', false);
g_form.setDisplay('u_configuration_item', false);

Setting a variable to be hidden and not mandatory using setDisplay() and setMandatory() does not work at the same time

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0718566

 

g_form.setDisplay('u_configuration_item', true);
g_form.setMandatory('u_configuration_item', true);