ServiceNow Client vs server scripting
The two worlds of ServiceNow code: client and server.
ServiceNow scripting starts with where the code runs. Client-side code runs in the browser on a form with g_form, g_user and g_scratchpad, as client scripts and UI policies. Server-side code runs on the instance with GlideRecord, current, previous and gs, as business rules, script includes and jobs. Try a UI policy before a client script.
- Client vs server, the fundamental divide
- Client-side script types
- The g_form API (client)
- Prefer no-code first
- Common mistakes
Most of ServiceNow is configured, not coded, but when configuration runs out, you script. The first and most important thing to master is where code runs, because client-side and server-side scripting have completely different APIs, powers and pitfalls. Get the location model right and everything else follows.
Client vs server, the fundamental divide
g_form, g_user, g_scratchpad. Types: Client Scripts, UI Policies.current, previous, gs. Types: Business Rules, Script Includes, jobs.Client-side script types
The g_form API (client)
GlideRecord or getReference call freezes the browser while it waits on the server. Use an async GlideAjax call to a Script Include instead, or move the logic server-side.Prefer no-code first
Before writing a client script, ask whether a UI policy does it (mandatory/read-only/visible by condition), it's declarative, faster, and survives upgrades better. Reserve client scripts for logic policies can't express.
Common mistakes
- Putting heavy logic client-side and freezing the form.
- Using
g_formon the server orGlideRecordon the client. - Synchronous
GlideAjax/getReferenceinstead of the async callback form. - Scripting what a UI policy or flow could do no-code.
▸ Press Run to execute.
Runs in a sandbox in your browser. Mock gs and GlideRecord and sample data are provided.