Skip to content
IT Canvass
Development · Lesson

Client scripts

Quick answer

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.

Key takeaways

  • 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

onLoad
Runs once when the form loads, set defaults, hide fields.
onChange
Runs when a specific field changes, the workhorse.
onSubmit
Runs when the form is saved, last-chance validation, can abort.
onCellEdit
Runs on inline list edits.

An onChange script

// Make "Justification" mandatory when priority is Critical function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue === '') return; g_form.setMandatory('justification', newValue == '1'); }

The g_form API is your toolbox: g_form.getValue(), setValue(), setMandatory(), setVisible(), setReadOnly(), addErrorMessage(). Use g_user to check roles on the client.

Do not query the database from the client. Client scripts have no direct database access. When you need server data, call a GlideAjax Script Include asynchronously, never synchronous 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 onChange with the isLoading check.
  • Move real logic to the server; the client should only shape the form.

Want to learn this properly?

Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. Client scripts run...

    • A. In the browser
    • B. On the database server
    • C. On the MID Server
    Show answer

    A. In the browser

    Client scripts execute in the user's browser.

  2. Which type reacts to a single field changing?

    • A. onChange
    • B. onLoad
    • C. onSubmit
    Show answer

    A. onChange

    onChange fires when its configured field changes.

  3. To fetch server data from a client script you should use...

    • A. GlideAjax (async)
    • B. Synchronous GlideRecord
    • C. Nothing, it can't
    Show answer

    A. GlideAjax (async)

    GlideAjax retrieves server data asynchronously without freezing the form.

Frequently asked questions

What does the term Client scripts refer to in ServiceNow?

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.

What is the practical takeaway on Client scripts?

Prefer UI policies for simple show/hide/mandatory, no code, faster. Always guard onChange with the isLoading check.
CallWhatsAppEnquire