Executing UI Policy on text field change

Pradnya6
Giga Contributor

Is there any way to execute UI policy on text field change. I have written a UI policy to check "Name" is not empty and the value entered is valid in "Execute if true" script. I have observed, this UI Policy executes only on form load and if I make the field blank and then enter value to it. This UI policy never execute if I change the value from "abc" to "xyz", because the revers condition is never satisfied. Is there I am missing anything ? As I want to execute this UI policy on change of text value. Please suggest.

3 REPLIES 3

Jacob_Andersen
ServiceNow Employee
ServiceNow Employee

The UI policy will run if the condition is satisfied and will work inversely if the condition is not satisfied. The problem that you're having is that you don't have a 'changes' condition for a field on a UI policy.

I'd simply use a client script to achieve what you're looking for. It should run OnChange for your particular field. The logic should be pretty simple from there on.

http://wiki.servicenow.com/index.php?title=Client_Scripts#onChange.28.29_Scripts


neetusingh
Giga Guru

'OnChange' Client Script would be the best solution for achieving this !!!!


geoffcox
Giga Guru

This history on this discussion is a little confusing, so I thought I would show agreement with Neetu and further clarify.

You are trying to test the validity of the contents of a field. A UI Policy can check for various editable conditions, but the conditions themselves are not easily scriptable. The Execute if True script only runs when the conditions get met, and the Execute if False script runs when the conditions are not met. The Execute scripts shouldn't be used to perform the conditions testing.

You haven't really said what you're going to do once the field contains valid data? This is important because if you simply don't want to let them save the record until it contains valid data, then you have options.

The simplest is to make the field mandatory, and have an onChange client script that tests the validity of the field. If the field is not empty but has invalid data, you should clear the field (so that the mandatory feature kicks in, preventing the user from saving a record with invalid data). You may also wish to display a friendly alert message explaining why the data was invalid.

Cheers,
Geoff.