ServiceNow Client Scripts
Client Scripts run JavaScript in the user's browser to make forms dynamic and interactive. While Business Rules handle server-side logic after a save, Client Scripts respond instantly to user actions on the form — showing or hiding fields, validating input, and populating values — without any round trip to the server.
Client Scripts vs. Business Rules
CLIENT SCRIPT BUSINESS RULE
Where runs: Browser Server (ServiceNow cloud)
When: During form use On save / query / delete
Response: Instant After a server call
Can query DB: No (limited) Yes (full access)
Can block save: Yes (onSubmit) Yes (setAbortAction)
User sees: Immediate feedback Messages after save
Types of Client Scripts
onLoad
Runs once when the form first opens. Use it to hide fields that should start hidden, set default values based on context, or show welcome messages for new records.
// onLoad: Hide the "Rejection Reason" field when form loads
function onLoad() {
g_form.setVisible('rejection_reason', false);
}
onChange
Runs whenever the value in a specific field changes. The script receives the field name, the old value, and the new value. Use it to show/hide other fields, populate related fields automatically, or warn the user about a selection.
// onChange: Show the urgency field only when category = "Hardware"
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == 'hardware') {
g_form.setVisible('urgency', true);
} else {
g_form.setVisible('urgency', false);
}
}
onSubmit
Runs when the user clicks Save or Submit. Use it for final validation checks before the form data reaches the server. Returning false from an onSubmit script cancels the save entirely and keeps the user on the form.
// onSubmit: Require description before submitting a high-priority incident
function onSubmit() {
var priority = g_form.getValue('priority');
var description = g_form.getValue('description');
if (priority == '1' && description == '') {
g_form.showErrorBox('description',
'Description is required for Critical incidents.');
return false; // Cancel the save
}
}
onCellEdit
Runs when a user edits a cell directly in a list view (inline editing). This type is less common and specifically handles list-level interactions rather than form-level ones.
The g_form API
The g_form object provides all the methods Client Scripts use to interact with the form. It is the primary tool in any Client Script.
Getting and Setting Values
g_form.getValue('field_name') // Read the field's stored value
g_form.getDisplayValue('field_name') // Read the visible display value
g_form.setValue('field_name', value) // Set the field to a new value
g_form.clearValue('field_name') // Erase the field's value
Showing and Hiding Fields
g_form.setVisible('field_name', true) // Show the field
g_form.setVisible('field_name', false) // Hide the field
Making Fields Mandatory or Read-Only
g_form.setMandatory('field_name', true) // Make the field required
g_form.setMandatory('field_name', false) // Make it optional
g_form.setReadOnly('field_name', true) // Prevent editing
g_form.setReadOnly('field_name', false) // Allow editing
Showing Messages on the Form
g_form.addInfoMessage('This record is read-only.')
g_form.addErrorMessage('Please fill in all required fields.')
g_form.showErrorBox('field_name', 'Invalid value entered.')
g_form.hideErrorBox('field_name')
A Practical Client Script Example
Scenario: On an incident form, when the user selects "VPN" as the category,
automatically populate the Assignment Group with "Network Support"
and make the "Remote Location" field visible and mandatory.
// onChange script on the "Category" field
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) { return; } // Don't run on initial load
if (newValue == 'vpn') {
g_form.setValue('assignment_group', 'a1b2c3d4...'); // sys_id of Network Support
g_form.setVisible('u_remote_location', true);
g_form.setMandatory('u_remote_location', true);
} else {
g_form.setVisible('u_remote_location', false);
g_form.setMandatory('u_remote_location', false);
}
}
The isLoading Flag
The isLoading parameter in an onChange script is true when the form first loads and populates its fields. Without checking this flag and returning early, the onChange script fires for every field as the form opens — running your logic at the wrong time and potentially causing errors. Always check if (isLoading) return; at the top of onChange scripts.
Performance Considerations
Client Scripts run in the user's browser. Too many Client Scripts on one form slow down page load and form interactions noticeably. Each script adds processing work to the browser. Administrators audit Client Scripts regularly using the JavaScript Debugger and the ServiceNow Performance Best Practice guidelines to identify and consolidate scripts that slow the user experience.
