Skip to main content

ServiceNow Client Script Functions

ServiceNow Client Scripts allow you to run JavaScript on the client-side (i.e., in the user's browser) to enhance the user interface experience. These scripts are typically used on forms and fields to manipulate UI elements, validate input, and auto-populate fields.


🧠 Client Script Types

Type Description
onLoad Executes when a form is loaded.
onChange Executes when a particular field's value changes.
onSubmit Executes when a form is submitted.
onCellEdit Executes when a cell is edited in a list (available in list editor).

🔧 Common Client Script Functions

1. g_form — Manipulate Form & Fields

The g_form object is used to control form fields.

Function Description
g_form.getValue('field_name') Gets the value of a field.
g_form.setValue('field_name', value) Sets the value of a field.
g_form.setDisplay('field_name', true/false) Shows or hides a field.
g_form.setVisible('field_name', true/false) (Alias of setDisplay)
g_form.setReadOnly('field_name', true/false) Makes a field read-only or editable.
g_form.setMandatory('field_name', true/false) Sets a field as mandatory or optional.
g_form.hideFieldMsg('field_name', true) Hides the field-level messages.
g_form.showFieldMsg('field_name', 'message', 'info/error/warning') Shows a message next to a field.
g_form.addInfoMessage('message') Adds an informational message to the top of the form.
g_form.addErrorMessage('message') Adds an error message to the top of the form.
g_form.clearMessages() Clears all messages from the form.
g_form.getReference('field_name', callback) Gets the referenced GlideRecord asynchronously.

2. g_user — Current User Information

Function Description
g_user.userID Sys ID of the current user.
g_user.name Full name of the current user.
g_user.firstName First name of the current user.
g_user.lastName Last name of the current user.
g_user.hasRole('role_name') Returns true if the user has the specified role.

3. g_scratchpad — Pass Server Info to Client

Useful when you want to pass data from a Script Include or business rule to the client script.

// Server Side Example:
g_scratchpad.someValue = 'Hello from server';
// Client Side:
alert(g_scratchpad.someValue);

4. GlideAjax — Call Server Scripts from Client

Used to call server-side Script Includes.

var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'myFunction');
ga.addParam('sysparm_input', g_form.getValue('field'));
ga.getXMLAnswer(function(response) {
  alert('Server returned: ' + response);
});

5. g_form.isNewRecord()

Returns true if the form is in "New Record" mode (i.e., not yet saved).


6. g_form.getSectionNames() and g_form.setSectionDisplay(section, visible)

Used to manipulate form sections (not commonly needed, but useful for dynamic forms).


✨ Example: onChange Script

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

  if (newValue === 'closed') {
    g_form.setDisplay('closure_notes', true);
    g_form.setMandatory('closure_notes', true);
  }
}

📌 Best Practices

  • Use isLoading checks in onChange scripts to avoid unwanted behavior on form load.
  • Avoid heavy operations in Client Scripts; offload logic to Script Includes via GlideAjax.
  • Minimize the number of client scripts to improve performance.
  • Use naming conventions like Client_OnLoad_ShowFields.

Let me know if you'd like a downloadable .md file or examples for a specific use case (e.g., GlideAjax with Script Include).