Client scripts
Summarize
Summary of Client scripts Building applications
Client scripts enable the execution of JavaScript on the client-side in response to events such as form loads, submissions, or field value changes. They are essential for configuring form behavior and enhancing user interactions within ServiceNow applications.
Show less
Key Features
- Control field visibility, editability, and requirements based on user roles.
- Set field values dynamically based on other fields' inputs.
- Modify choice list options according to user roles.
- Display contextual messages based on field values.
Client scripts operate only on forms and search pages, with specific types including:
- onLoad(): Executes upon form rendering.
- onSubmit(): Validates form submissions.
- onChange(): Responds to specific field value changes.
- onCellEdit(): Triggers during cell edits in list views.
Key Outcomes
Utilizing client scripts can significantly enhance user experience by ensuring that forms behave intuitively and responsively. However, they are not a substitute for data protection measures; use ACLs and data policies to secure sensitive information.
New scripts are created in strict mode, limiting direct DOM access and the use of global objects to maintain security and integrity. Understanding and correctly implementing client scripts can lead to more effective application development and user satisfaction.
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, with direct DOM access disabled. Access to jQuery, prototype, and the window object are also disabled. To disable this on a per-script basis, configure this form and select the Isolate script check box. To disable this feature for all new globally-scoped client-side scripts set the system property glide.script.block.client.globals to false. |