Data Migration and Integration · Topic 4 of 4

Scripting in ServiceNow

Scripting in ServiceNow for admins: client scripts versus server scripts, g_form, GlideRecord, gs, script includes, UI actions and when to script at all.

  • 4 min
In a nutshell
  • Client scripts run in the browser; server scripts run on the instance.
  • Client script types: onLoad, onChange, onSubmit, onCellEdit.
  • g_form and g_user are client objects; GlideRecord, gs and current are server objects.

What it is

ServiceNow scripts are written in JavaScript. An administrator does not need to be a developer, but must recognize the two kinds of scripts and know which fits a requirement.

The central question is always where the code runs: in the user's browser (client side) or on the server.

RequirementDeclarative option firstUI policy, flow, business ruleStill not enough?Client or server script

How it works

Client side and server side

Client sideServer side
Runs inThe user's browserThe instance
ExamplesClient scripts, UI policy scriptsBusiness rules, script includes, scheduled jobs
Main objectsg_form, g_userGlideRecord, gs, current
Good forForm behavior while the user worksData logic, validation, integrations

Client scripts can be skipped by a list edit or an import. Server scripts run no matter where the change came from.

Client scripts

A client script is created under System Definition > Client Scripts. Its type decides when it runs.

TypeRuns when
onLoadThe form loads
onChangeA chosen field changes
onSubmitThe form is saved or submitted
onCellEditA value is edited in a list

Typical client scripts use g_form to work with the form, for example g_form.setValue, g_form.getValue, g_form.setMandatory, g_form.setVisible and g_form.setReadOnly. g_user gives information about the logged in user, such as their roles.

Server scripts

The GlideRecord class reads and writes records on the server. A script builds a query, runs it and loops over the results.

A typical pattern is a query on the Incident table with a filter, then a loop that reads or changes each matching record. Methods you should recognize include addQuery, query, next, get, insert, update and deleteRecord.

gs is the GlideSystem object. It offers helpers such as gs.info to write to the log and gs.getUserID for the current user. In a business rule, current is the record being processed.

Script includes and UI actions

A script include stores reusable server code, so many scripts can call it instead of repeating it. A client script cannot call it directly unless the script include is marked client callable and called with GlideAjax.

A UI action adds a button, link or context menu item to a form or list. It can run client code, server code, or both.

Background scripts and logging

Administrators sometimes test short server scripts in System Definition > Scripts - Background. Treat it with care because the script runs for real against live data.

Use gs.info and the system log to see what a script did when something misbehaves.

Choosing the right tool

RequirementPrefer
Show or require a field based on a conditionUI policy
Multi-step processFlow in Workflow Studio
Set values on save from any sourceBusiness rule
React while a user changes a form fieldClient script
Reuse the same server code in many placesScript include

Use scripting only when the no-code option cannot meet the need. Less code means less to maintain and upgrade.

How the exam asks about this

  • "Warn the user when they change Priority on a form." An onChange client script.
  • "Update all open incidents for a user in code on the server." A server script using GlideRecord.
  • "Reuse the same logic in several business rules." A script include.

What to remember for the exam

  • Client scripts run in the browser; server scripts run on the instance.
  • Client script types: onLoad, onChange, onSubmit, onCellEdit.
  • g_form and g_user are client objects; GlideRecord, gs and current are server objects.
  • Script includes hold reusable server code.
  • UI actions add buttons, links and menu items.
  • Choose a declarative feature before writing a script.
Watch out

Do not use GlideRecord in a client script to read other records. Client code calls the server through GlideAjax or a display business rule.

Common mistakes

Using a client script for security. Client code can be bypassed, so access rules belong in ACLs.

Mixing up g_form and current. g_form works with the open form in the browser; current is the record in a server-side rule.

Scripting a simple field rule. A UI policy or business rule action solves it with no code to maintain.

Check yourself

  • Which scripts run in the browser?

    Client scripts and UI policy scripts, using objects such as g_form and g_user.

  • What class do server scripts use to query and update records?

    GlideRecord.

  • Where do you store server code reused by many scripts?

    In a script include.

Topic 4 of 4 in Data Migration and Integration

Test yourself on Data Migration and Integration

The full ServiceNow CSA question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by ServiceNow. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.