ServiceNow Client scripts
Run JavaScript in the browser to react to form and field events in real time.
ServiceNow client scripts run JavaScript in the browser on a form, the counterpart of server-side business rules. Four types: onLoad when the form opens, onChange when one field changes, onSubmit as last-chance validation that can abort, onCellEdit for inline list edits. The g_form API sets values, mandatory and visibility; guard onChange with isLoading and keep logic on the server.
- The four types
- An onChange script
- Keep them light
- Try it Yourself
Client scripts run in the user's browser on a form, letting you validate input, show or hide fields, and react the instant a value changes, without a round-trip to the server. They are the counterpart to server-side business rules: same language (JavaScript), opposite side of the wire.
The four types
An onChange script
The g_form API is your toolbox: g_form.getValue(), setValue(), setMandatory(), setVisible(), setReadOnly(), addErrorMessage(). Use g_user to check roles on the client.
getReference or GlideRecord on the client, which freezes the form.Keep them light
- Prefer UI policies for simple show/hide/mandatory, no code, faster.
- Always guard
onChangewith theisLoadingcheck. - Move real logic to the server; the client should only shape the form.
▸ Press Run to execute.
Runs in a sandbox in your browser. Mock gs and GlideRecord and sample data are provided.