← All guides

ServiceNow UI Policy vs Client Script vs Data Policy: Which to Use

UI policy vs client script vs data policy in ServiceNow: what each one does, where it runs, and a simple rule for choosing the right one for a field.

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 policyClient scriptData policy
Where it runsBrowserBrowserServer (optionally browser too)
Tablesys_ui_policysys_script_clientsys_data_policy2
Needs codeNo (scripts optional)YesNo
MandatoryYesYesYes
Read-onlyYesYesYes
Visible / hiddenYesYesNo
Set values, messages, server callsOnly through its scriptsYesNo
Reverses when condition is falseYes, with Reverse if falseOnly if you code itNot applicable
Applies to import setsNoNoYes, if Apply to import sets is selected
Applies to web servicesNoNoSOAP, 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:

  1. 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.
  2. Is it only mandatory, read-only or visible, based on field values? Use a UI policy.
  3. Does it set values, talk to the server, show messages or need real logic? Use a client script.

Some common cases:

RequirementUse
Close notes mandatory when state is Resolved, from every channelData policy with Use as UI Policy on client
Hide the vendor fields unless category is HardwareUI policy
Clear the assignee when the group changesClient script (onChange)
Short description read-only after the record is createdData policy, or UI policy if only the form matters
Default the location to the caller's locationClient script with GlideAjax, or a server-side default
End date must be after start dateBefore 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.

Frequently asked questions

What is the difference between a UI policy and a data policy in ServiceNow?

A UI policy runs in the browser and controls whether form fields are mandatory, read-only or visible. A data policy runs on the server and enforces mandatory and read-only rules on the data itself, so it can also apply to import sets and SOAP web services. A UI policy can be skipped by anything that does not use the form; a data policy cannot be skipped by a form user.

When should I use a client script instead of a UI policy?

Use a client script when you need logic a condition builder cannot express: setting or clearing values, showing messages, confirming with the user, or calling the server with GlideAjax. If all you need is mandatory, read-only or visible based on field values, a UI policy is simpler and reverses itself automatically.

Can I convert a UI policy into a data policy?

Yes. A UI policy form has a related link to convert it to a data policy, and a data policy form has a link to convert it to a UI policy. Conversion only works when the policy fits the target's limits. A UI policy that uses scripts or controls visibility has parts a data policy cannot carry, because data policies only support mandatory and read-only.

Do client scripts and UI policies run for imports and REST calls?

No. Client scripts and UI policies run in the browser on forms. Import sets, REST and SOAP calls, and server scripts never load the form, so they skip both. Enforce rules that must always hold with a data policy or a before business rule.

More practical ServiceNow notes.

Back to the guides