The short answer to UI policy vs client script vs data policy in ServiceNow: use a UI policy to make form fields mandatory, read-only or hidden based on conditions, use a client script when the form needs logic a condition cannot express, and use a data policy when a mandatory or read-only rule must hold no matter how the record arrives. The first two only run in the browser. The third runs on the server.
Most mistakes come from treating the browser tools as enforcement. A mandatory field set by a UI policy stops a user on the form. It does not stop an import, a REST call or a background script.
The three tools at a glance
| UI policy | Client script | Data policy | |
|---|---|---|---|
| Where it runs | Browser | Browser | Server (optionally browser too) |
| Table | sys_ui_policy | sys_script_client | sys_data_policy2 |
| Needs code | No (scripts optional) | Yes | No |
| Mandatory | Yes | Yes | Yes |
| Read-only | Yes | Yes | Yes |
| Visible / hidden | Yes | Yes | No |
| Set values, messages, server calls | Only through its scripts | Yes | No |
| Reverses when condition is false | Yes, with Reverse if false | Only if you code it | Not applicable |
| Applies to import sets | No | No | Yes, if Apply to import sets is selected |
| Applies to web services | No | No | SOAP, if Apply to SOAP is selected |
UI policies: conditional form behaviour without code
You find UI policies under System UI > UI Policies, or in the UI Policies related list on a table. A UI policy has a condition and a set of UI Policy Actions. Each action sets a field to mandatory, visible or read-only as true, false or leave alone.
Settings worth knowing:
- On load applies the policy when the form opens as well as when fields change.
- Reverse if false undoes the actions when the condition stops being true. Leave it selected unless you have a reason not to.
- Global applies the policy to every view. Clear it to target one view.
- Inherit applies the policy to tables that extend this one.
- Order decides which policy wins when two policies touch the same field. The higher order runs later and wins.
UI policies can also run scripts. In the Advanced view, select Run scripts and fill in Execute if true and Execute if false. Each one is an onCondition() function:
// Execute if true: category is "Network"
function onCondition() {
g_form.showFieldMsg('cmdb_ci', 'Select the affected network device.', 'info');
}
// Execute if false
function onCondition() {
g_form.hideFieldMsg('cmdb_ci', true);
}
If a UI policy needs more than a few lines of script, it is usually a client script in disguise. Move it.
Client scripts: logic the condition builder cannot express
Client scripts live under System Definition > Client Scripts. They run on load, on change of one field, on submit, or on cell edit in a list. Use them to:
- Set or clear values when another field changes
- Show messages or ask the user to confirm
- Fetch server data with GlideAjax or read
g_scratchpad - Validate a value against a pattern before submit
A typical onChange script clears a dependent field:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === oldValue) {
return;
}
// A new assignment group makes the old assignee meaningless
g_form.clearValue('assigned_to');
}
This is the same pattern as the clear assignee on assignment group change script in the library. Notice it sets a value, which a UI policy action cannot do.
Do not use a client script to show, hide or require fields on a simple condition. You would have to write the reverse branch yourself, and the next admin will look for a UI policy first. The client script best practices guide covers the rest of the rules for writing them well.
Data policies: rules that hold for every record
Data policies live under System Policy > Rules > Data Policies. A data policy has a condition and a set of rules that make fields mandatory or read-only. When a record that breaks the policy reaches the server, the save is rejected with a data policy error.
The options that matter:
- Apply to import sets enforces the policy on data loaded through import sets, including web service import sets.
- Apply to SOAP enforces it on data from SOAP web services. ServiceNow's documentation states that this option does not cover scripted SOAP services and does not affect REST. If a REST integration must respect a rule, test it on your release or back the rule with a business rule.
- Use as UI Policy on client also applies the policy on the form, through the UI policy engine. This gives users the same mandatory and read-only markers they would get from a UI policy, so you do not need a second, duplicate UI policy.
Data policies have limits. They cannot hide fields, run scripts or validate values beyond "has a value" and "cannot change". For a rule like "planned end must be after planned start", use a before business rule:
// Before business rule on change_request, insert and update
(function executeRule(current, previous /*null when async*/) {
var start = current.getValue('start_date');
var end = current.getValue('end_date');
if (start && end && new GlideDateTime(end).before(new GlideDateTime(start))) {
gs.addErrorMessage('Planned end date must be after planned start date.');
current.setAbortAction(true);
}
})(current, previous);
The validate planned change dates script shows a fuller version, and the business rule best practices guide explains when a before rule is the right place for this.
How to choose
Ask three questions in order:
- Must the rule hold for imports, integrations or scripts? Use a data policy for mandatory and read-only, or a before business rule for anything else. Add a browser tool only if users need a friendlier message on the form.
- Is it only mandatory, read-only or visible, based on field values? Use a UI policy.
- Does it set values, talk to the server, show messages or need real logic? Use a client script.
Some common cases:
| Requirement | Use |
|---|---|
| Close notes mandatory when state is Resolved, from every channel | Data policy with Use as UI Policy on client |
| Hide the vendor fields unless category is Hardware | UI policy |
| Clear the assignee when the group changes | Client script (onChange) |
| Short description read-only after the record is created | Data policy, or UI policy if only the form matters |
| Default the location to the caller's location | Client script with GlideAjax, or a server-side default |
| End date must be after start date | Before business rule, plus an optional onChange script for instant feedback |
Conflicts and execution order
When a client script and a UI policy both change the same field, the result depends on which one ran last. On load, client scripts normally run before UI policies, so the UI policy usually wins. That is not a design you should depend on. Give each field one owner: if a UI policy controls whether close_notes is mandatory, no client script should touch its mandatory state.
To find what already touches a table, run this read-only background script before adding another rule:
// Lists active UI policies and client scripts on a table
var table = 'incident';
var policy = new GlideRecord('sys_ui_policy');
policy.addQuery('table', table);
policy.addActiveQuery();
policy.orderBy('order');
policy.query();
while (policy.next()) {
gs.info('UI policy [' + policy.getValue('order') + ']: ' + policy.getValue('short_description'));
}
var cs = new GlideRecord('sys_script_client');
cs.addQuery('table', table);
cs.addActiveQuery();
cs.query();
while (cs.next()) {
gs.info('Client script (' + cs.getValue('type') + '): ' + cs.getValue('name'));
}
Check data policies for the same table in the Data Policies list before you add a UI policy that duplicates one.
Converting between UI and data policies
A UI policy form has a Convert this to Data Policy related link, and a data policy has Convert this to UI Policy. Conversion is handy when a form-only rule turns out to need server enforcement. It only works cleanly for policies that use mandatory and read-only actions without scripts, because a data policy cannot carry visibility or script logic. Deactivate the original after converting, or you end up with two rules doing the same job.
A short checklist
- Mandatory or read-only that must always hold: data policy
- Same rule needed on the form: select Use as UI Policy on client instead of building a duplicate UI policy
- Show, hide or require on a condition, form only: UI policy with Reverse if false
- Values, messages, server calls: client script
- Validation beyond "has a value": before business rule
- One owner per field state, no overlapping rules
- Test imports and integrations against the rule, not just the form
Get this choice right and most "the field was mandatory but the record saved anyway" defects disappear. Add an ATF test that inserts a record server side as well as one that uses the form, so both paths stay covered.