Function: UiPolicy(config)
Creates a UI Policy that defines dynamic form behaviors to change field properties based on conditions. UI Policies can make fields mandatory, read-only, visible, hidden, or cleared when certain conditions are met.
Parameters
config
UiPolicy<keyof Tables>
an object containing api properties:
Properties:
-
$id (required):
string | number | ExplicitKey<string> -
shortDescription (required):
stringA brief description of what the UI Policy does (required and must not be empty) -
actions (optional):
UiPolicyAction<keyof Tables>[]List of actions to perform when the UI Policy condition is met -
active (optional):
booleanWhether the UI Policy is active -
conditions (optional):
stringA condition script or query that determines when the UI Policy applies -
description (optional):
stringDetailed description of the UI Policy -
global (optional):
booleanWhether the UI Policy applies globally across all applications -
inherit (optional):
booleanWhether the UI Policy is inherited by tables that extend this table -
isolateScript (optional):
booleanWhether to run the script in an isolated scope -
modelId (optional):
stringModel ID for the UI Policy -
modelTable (optional):
keyof TablesModel table for the UI Policy -
onLoad (optional):
booleanWhether the UI Policy runs when the form loads -
order (optional):
numberOrder/priority of the UI Policy execution -
relatedListActions (optional):
UiPolicyRelatedListAction[]List of related list visibility controls -
reverseIfFalse (optional):
booleanWhether to reverse the actions when the condition is false (renamed from 'reverse' for clarity) -
runScripts (optional):
booleanWhether to run the scripts defined in scriptTrue/scriptFalse -
scriptFalse (optional):
stringJavaScript code that runs when the condition evaluates to false -
scriptTrue (optional):
stringJavaScript code that runs when the condition evaluates to true -
setValues (optional):
stringValues to set when the UI Policy is applied -
table (optional):
keyof TablesThe table to which the UI Policy applies (optional) -
uiType (optional):
uiTypeUser interface to which the UI Policy applies -
view (optional):
string | Record<'sys_ui_view'>View context where the UI Policy applies (sys_ui_view reference or name). If global is true, the UI Policy applies to all form views. If global is false/undefined, specify a view to make the policy view-specific.
See
Examples
Basic UI Policy
Create a minimal UI policy for a table
/**
* @title Basic UI Policy
* @description Create a minimal UI policy for a table
*/
import { UiPolicy } from '@servicenow/sdk/core'
UiPolicy({
$id: Now.ID['basic_policy'],
table: 'incident',
shortDescription: 'Basic UI Policy Test',
})
UI Policy with Field Actions
Create a UI policy that controls field visibility, mandatory state, and read-only behavior
/**
* @title UI Policy with Field Actions
* @description Create a UI policy that controls field visibility, mandatory state, and read-only behavior
*/
import { UiPolicy } from '@servicenow/sdk/core'
UiPolicy({
$id: Now.ID['policy_with_actions'],
table: 'incident',
shortDescription: 'Policy with Actions',
actions: [
{
field: 'priority',
visible: false,
mandatory: 'ignore',
readOnly: 'ignore',
},
{
field: 'urgency',
visible: 'ignore',
mandatory: true,
readOnly: false,
},
],
})
UI Policy with Scripts
Create a UI policy with condition-based scripts and advanced options
/**
* @title UI Policy with Scripts
* @description Create a UI policy with condition-based scripts and advanced options
*/
import { UiPolicy } from '@servicenow/sdk/core'
UiPolicy({
$id: Now.ID['policy_with_scripts'],
table: 'incident',
shortDescription: 'Policy with Scripts',
runScripts: true,
uiType: 'desktop',
scriptTrue: 'function onCondition() { g_form.addErrorMessage("Error"); }',
scriptFalse: 'function onCondition() { g_form.clearMessages(); }',
conditions: 'state=1',
global: true,
reverseIfFalse: true,
isolateScript: true,
})