ServiceNow client script cheat sheet

Client scripts run in the browser. These are the four templates, plus g_user (the logged-in user) and g_scratchpad (data sent from a display business rule).

Client7 methods · examples checked with the Script Checker · reviewed October 2026

onLoad()

#

Runs once when the form opens.

function onLoad() {
    if (g_form.isNewRecord()) {
        g_form.setValue('impact', 3);
    }
}

onChange(control, oldValue, newValue, isLoading, isTemplate)

#

Runs when the chosen field changes. Keep the isLoading check: it also runs while the form loads.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.setValue('u_previous_category', oldValue);
}

onSubmit()

#

Runs when the form is saved. Return false to stop the save.

function onSubmit() {
    if (g_form.getValue('state') == '6' && g_form.getValue('close_notes') === '') {
        g_form.addErrorMessage('Add close notes before resolving.');
        return false;
    }
}

Do the check with data already on the form: a GlideAjax answer arrives after the form is submitted.

onCellEdit(sysIDs, table, oldValues, newValue, callback)

#

Runs when a list cell is edited. Call callback(true) to save or callback(false) to cancel.

function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
    // State 7 (Closed) can only be set from the form
    callback(newValue != '7');
}

g_form is not available while editing a list.

g_user.userID / userName / getFullName()

#

The logged-in user's sys_id, user name and full name, without a server call.

function onLoad() {
    if (g_form.isNewRecord()) {
        g_form.setValue('caller_id', g_user.userID, g_user.getFullName());
    }
}

g_user.hasRole('role') / hasRoleExactly('role')

#

hasRole() is true for admins as well; hasRoleExactly() ignores the admin role.

function onLoad() {
    if (!g_user.hasRole('itil')) {
        g_form.setReadOnly('priority', true);
    }
}

Fine for showing and hiding, but enforce access with ACLs on the server.

g_scratchpad

#

Reads values that a display business rule put on g_scratchpad: no extra server call.

function onLoad() {
    if (g_scratchpad.isVip) {
        g_form.addInfoMessage('VIP caller: handle with priority.');
    }
}

The values are set when the form loads; they do not change while the user edits.

Frequently asked questions

Why does my onChange client script run when the form loads?

onChange scripts also run during form load, with isLoading set to true. Keep the template’s first lines: if (isLoading || newValue === '') { return; }.

How do I get data from the server into a client script?

For data needed when the form opens, set it on g_scratchpad in a display business rule. For data that depends on what the user changes, use GlideAjax. Avoid GlideRecord in client scripts.