Client scripts
Summarize
Summary of Client scripts
Client scripts in ServiceNow enable JavaScript execution in the user's web browser in response to client-side events, such as form loading, field changes, or form submission. They help customize and control form behavior dynamically to enhance the user experience by modifying field visibility, accessibility, values, and choice options based on context like user roles.
Show less
However, client scripts are not intended as a security measure to restrict data access. To protect sensitive data, use Access Control Lists (ACLs) or Data Policies.
Where Client Scripts Run
Client scripts generally apply to forms and search pages but do not affect list views except for onCellEdit() scripts. For list editing control, customers should use ACLs, business rules, data policies, or disable list editing.
Note that client scripts are not supported on ServiceNow mobile applications.
Key Client Script Types and Use Cases
- onLoad(): Executes when a form is first rendered, typically to manipulate the form or set defaults before user input.
- onSubmit(): Executes upon form submission, commonly used for validation and can cancel submission by returning false.
- onChange(): Runs when a specific field value changes; enables dynamic responses based on user input. Parameters include control, oldValue, newValue, isLoading, and isTemplate.
- onCellEdit(): Executes when a cell value is changed in list editing mode, allowing validation and control over committing changes.
Important Configuration Fields
- Name: Identifier for the client script.
- Table: Specifies the table the script applies to.
- UI Type: Defines target interface(s): Desktop only, Mobile/Service Portal only, or All.
- Field Name: Relevant for scripts triggered by field changes.
- Application: Application scope of the script.
- Active: Controls whether the script is enabled.
- Global and View: Determine if the script runs on all views or specific views only.
- Description: Explains the script's purpose and functionality.
- Messages: Localized strings accessible via
getMessage()for user-friendly messaging. - Script: Contains the actual JavaScript code.
- Isolate script: New scripts run in strict mode by default, restricting direct DOM and global object access to improve security and stability. This can be disabled per script if needed.
Practical Considerations for ServiceNow Customers
- Use client scripts to enhance form interactivity and guide user input in real time.
- Do not rely on client scripts for data security; always implement ACLs and data policies to protect sensitive information.
- Choose the appropriate client script type based on when and how you want to trigger your logic.
- Be aware that client scripts do not run on mobile apps, so plan accordingly for mobile users.
- Manage script activation and scope carefully to ensure the correct behavior across different UI types and views.
- Utilize localized messages to support multi-language environments and improve user communication.
Client scripts allow the system to run JavaScript on the client (web browser) when client-based events occur, such as when a form loads, after form submission, or when a field changes value.
Use client scripts to configure forms, form fields, and field values while the user is using the form. Client scripts can:
- make fields hidden or visible
- make fields read only or writable
- make fields optional or mandatory based on the user's role
- set the value in one field based on the value in other fields
- modify the options in a choice list based on a user's role
- display messages based on a value in a field
Client scripts are intended to optimize the user experience on a form. Client scripts are not meant to protect unwanted access to data.
To prevent unwanted access to data, ensure that sensitive fields are hidden or read-only through ACLs or data policies.
For more information, see Access Control List Rules or Data policy.
Where client scripts run
- Create an access control to restrict who can edit field values.
- Create a business rule to validate content.
- Create a data policy to validate content.
- Create an onCellEdit() client script to validate content.
- Disable list editing for the table.
Client script form
| Field | Description |
|---|---|
| Name | Name of the client script. |
| Table | Table to which the client script applies. |
| UI Type | Target user interface to which the client script applies.
|
| Type | onLoad() — runs when the system first renders the form and before users can enter data. Typically, onLoad() client scripts perform client-side-manipulation of the current form or set default record values. onSubmit() — runs when a form is submitted. Typically, onSubmit() scripts validate things on the form and ensure that the submission makes sense. An onSubmit() client script can cancel form submission by returning a value of false. onChange() — runs when a particular field value changes on the form.
The onChange() client script must specify these parameters.
onCellEdit() — runs when the list editor changes a cell value. The
onCellEdit() client script must specify these parameters.
|
| Field Name | Name of the field to which the script applies. Available only if the script responds to a field value change (onChange or onCellEdit script types). |
| Application | Application where this client script resides. |
| Active | Enables the client script when selected. Unselect this field to disable the client script. |
| Inherited | Indicates whether the client script applies to extended tables. |
| Global | If true, the client script runs on all views of the table. |
| View | Only visible when Global is unselected. Views on which the client script will run. |
| Description | Content describing the functionality and purpose of the client script. |
| Messages | Text string (one per line) available to the client script as localized messages using getmessage('[message]'). For additional information, see Translate a client script message. |
| Script | Contains the client script. |
| Isolate script | New client scripts are run in strict mode, in which direct DOM access is turned off. Access to jQuery, prototype, and the window object are also turned off by default. To enable DOM access on a per-script basis, leave the Isolate script option cleared. To turn off strict mode for all new globally scoped client scripts, set the glide.script.block.client.globals system property to false. |