IT CanvassTalk to an advisor
Development · LessonBy , ServiceNow Trainer, 8 yrs · Published · current release · beginner

Client scripts

Run JavaScript in the browser to react to form and field events in real time.

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.
Try it YourselfJavaScript
▸ Press Run to execute.

Runs in a sandbox in your browser. Mock gs and GlideRecord and sample data are provided.

Practice challenge

+0 XPStreak ×0
Question 1 of 3
Client scripts run...

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.
Practise this on your own free instance.
Set up your free instance →
Already working on ServiceNow and stuck on a live ticket?Get an expert ServiceNow developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support