Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Complete Guide to Client Scripts in ServiceNow (Beginner → Advanced)

Tushar8649
Giga Contributor

 


🔹 What is a Client Script?

A Client Script is JavaScript that runs on the browser (client side) and controls how the form behaves.

Why we use it:

  • Improve user experience

  • Validate data before submission

  • Automate field behavior

  • Reduce unnecessary server calls


🔹 Types of Client Scripts

ServiceNow mainly provides 4 types:


1. onLoad()

Runs when the form loads.

function onLoad() {
   g_form.setValue('priority', 2);
}

Use cases:

  • Set default values

  • Hide/show fields

  • Initialize form data


2. onChange()

Runs when a field value changes.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue === '') return;

   if (newValue == '1') {
      g_form.setValue('impact', 1);
   }
}

Use cases:

  • Auto-populate fields

  • Apply dependent logic

  • Dynamic updates


3. onSubmit()

Runs when user submits the form.

function onSubmit() {
   if (!g_form.getValue('short_description')) {
      alert('Short description is required');
      return false;
   }
   return true;
}

Use cases:

  • Validation before submission

  • Prevent incorrect data


4. onCellEdit()

Used in list view (not form).

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
   if (!newValue) {
      alert('Value cannot be empty');
      return false;
   }
   callback(true);
}

Use cases:

  • Validate inline editing

  • Control list-level changes


🔹 g_form (Most Important API)

This is the main object used in client scripts.

Common methods:

  • getValue() → get field value

  • setValue() → set field value

  • setDisplay() → show/hide field

  • setMandatory() → make field mandatory

  • setReadOnly() → make field read-only

  • addInfoMessage() → show info message

  • addErrorMessage() → show error

Example:

g_form.setMandatory('short_description', true);
g_form.setReadOnly('priority', true);

🔹 Execution Flow

Simple way to understand:

Form loads → onLoad()
Field changes → onChange()
Form submit → onSubmit()
List edit → onCellEdit()

🔹 Client vs Server Scripts

Client Script Server Script

Runs on browserRuns on server
Fast executionSlightly slower
No DB access

Full DB access

UI logicData processing

🔹 Best Practices

  • Always use:

if (isLoading || newValue === '') return;
  • Keep logic simple and readable

  • Avoid heavy processing

  • Use GlideAjax for server calls

  • Prefer UI Policies for simple conditions


🔹 Common Mistakes

  • Using GlideRecord in client script

  • Missing isLoading check

  • Too many alerts

  • Writing large logic in onChange


🔹 Real Use Cases

  • Auto set Impact and Urgency

  • Validate mandatory fields

  • Auto assign group

  • Show/hide fields based on conditions


🔹 GlideAjax (Client → Server)

When you need server data:

var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getData');
ga.getXMLAnswer(function(response) {
   g_form.setValue('field', response);
});

🔹 Final Thought

Client Scripts are mainly about:

Better UI + Real-time validation + Smart forms

If your forms feel dynamic and responsive, good client scripting is usually behind it.


If anyone has real project scenarios or edge cases, feel free to share. Always interesting to see how others are solving problems.

1 REPLY 1

saraw925
Tera Contributor

Nice. Thank you for sharing.