Will a UI Policy's 'Execute if true' script only run at load time? It doesn't seem to run whenever the fields are changed.

garyopela
ServiceNow Employee
ServiceNow Employee

For example, on the project task, I created a test UI Policy below:

 

function onCondition() {

     

if (g_form.getValue('time_constraint') == 'asap') {

  g_form.setReadonly('end_date', true);

}

}

 

This very simple rule doesn't change the read only of the end_date field whenever the time_constraint field is updated. It will, however, work properly if you change the time_constraint and then save the form. When the form is reloaded, the field is then properly read only or not.

4 REPLIES 4

jamie_girouard
Giga Expert

There is no reason that I can think of why that wouldn't work.   I tried a similar function on our test instance and it worked fine.



The only thing that throws me every once in a while is that the condition in the UI policy needs to be true before the UI policy script will be invoked.   So if, for example, your condition on your test UI policy is that State = "Resolved", the state value must be Resolved before the UI policy script will run.   It won't work the other way.



In other words, think of the UI policy as the simple way to create a javascript onChange function.



Hope that helps.


Bhavesh Jain1
Giga Guru

UI Policy will work like an onChange client script only if there is a condition associated with the UI policy something like State = Resolved. So now when the State changes to Resolved, you UI policy will get triggered.


Also if you change the state to InProgress, the 'reverse if false' of UI policy will get triggered and your UI policy will run and will revert all the UI policy actions. So in both the case the UI policy will run but you will have to ensure that the condition field is using your field which is changing.


Shane J
Tera Guru

Gary, what did you end up doing here?   I'm having a similar situation when using a UI Action - it triggers and beats out the UI Policy.



In this case, I'm trying to enforce Mandatory fields when the State is changed, but the UI Action switches the State and saves the record.   If you go back, the enforcement is in place.  


garyopela
ServiceNow Employee
ServiceNow Employee

Hey, we just ended up using a client script.