Client Scripts

Call Server-Side Code from a Client Script

Client scripts can't run GlideRecord queries or access server-side APIs directly — that's by design. This guide shows you how to call server-side code from a client script using GlideAjax without triggering a page reload.

Why client scripts need server-side calls

Client scripts run in the user's browser, which means they can't access the ServiceNow database or server-side APIs. Before GlideAjax, developers worked around this by submitting forms to trigger Business Rules or using synchronous server calls that locked up the UI. Form admins and developers needed a way to populate fields dynamically, validate user input against database records, or update related records without forcing users through a clunky page reload cycle.

How GlideAjax works

GlideAjax creates an asynchronous connection between your client script and a server-side Script Include. You write all the database logic in a Script Include that extends AbstractAjaxProcessor, then call it from your client script using the GlideAjax constructor. The server runs your logic and sends the result back to a callback function in your client script. This keeps the UI responsive while giving you full server-side access — GlideRecord, system properties, anything you need.

Building production-quality AJAX calls

Basic GlideAjax works with a single function that returns a string. Production implementations add error handling in the callback, parameter validation in the Script Include, and multiple functions in the same Script Include to group related server-side operations. You'll also want to handle cases where the server call fails or takes too long, and consider caching results client-side if users might trigger the same call repeatedly.

Before you start

  • script_writer role (to create Script Includes)
Sourdough
Chrome Extension

Sourdough: ServiceNow Monitoring and Analytics

A Chrome extension for ServiceNow Admins and Developers with essential tools, analytics, graphs and monitoring features.

Instance HealthGraphs & ChartsAPI HealthDeveloper ToolsQuick SearchInstance Switcher
Add to Chrome

Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.

Overview
Tasks
CMDB
API
Metrics
Monitor
Internals
Instance:sourdoughdev·Version:Yokohama
Instance StateONLINE
System StatusFully Operational
Session Timeout90 minutes
Logged-In Sessions2 (20 active)
Build Nameyokohama-12-18-2024_p1
IP Address10.159.128.43
Instance HealthHealth Score: 90%
🔥 5dSourdough (Chrome Plugin)Dark Mode

Step by step

1

Create the Script Include

Navigate to System Definition > Script Includes and click New. Set the Name to something descriptive like 'UserValidationAjax'. Check both Client callable and Active boxes. In the Script field, start with 'var UserValidationAjax = Class.create(); UserValidationAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {});'. The extend() call is what makes this callable from client scripts.

TIP

Use a naming convention that clearly identifies AJAX Script Includes — many teams suffix with 'Ajax' or 'Processor'.

2

Add your server-side function

Inside the curly braces of your Script Include, add a function like 'checkUserExists: function() { var userId = this.getParameter('user_id'); var gr = new GlideRecord('sys_user'); gr.addQuery('user_name', userId); gr.query(); return gr.hasNext().toString(); }'. Always use this.getParameter() to get values from the client script, and always return a string — return booleans or objects as strings and parse them client-side.

3

Add the type declaration

After the closing brace of your prototype object, add the type declaration: 'UserValidationAjax.prototype.type = 'UserValidationAjax';'. This must exactly match your Script Include name. Without this line, ServiceNow can't route calls to your Script Include and you'll get silent failures.

4

Call it from your client script

In your client script, create the GlideAjax object: 'var ga = new GlideAjax('UserValidationAjax');'. Add any parameters with 'ga.addParam('sysparm_name', 'checkUserExists');' and 'ga.addParam('user_id', g_form.getValue('assigned_to'));'. The sysparm_name parameter tells ServiceNow which function to call in your Script Include.

5

Set up the callback function

Define your callback function before the getXMLAnswer call: 'function handleResponse(response) { var answer = response.responseXML.documentElement.getAttribute('answer'); if (answer == 'true') { g_form.addInfoMessage('User found'); } }'. The response always comes back as XML, and your return value is in the 'answer' attribute.

6

Execute the AJAX call

Trigger the server call with 'ga.getXMLAnswer(handleResponse);'. This is asynchronous — code after this line will run immediately while the server processes your request. Don't try to use the response data outside the callback function or you'll get undefined values.

TIP

Test AJAX calls in onChange client scripts first — they're easier to trigger and debug than onLoad or onSubmit scripts.

Best practices

  • Always validate parameters in your Script Include before using them in GlideRecord queries — client-side data can be manipulated by users.

  • Return structured data as JSON strings and use JSON.parse() in your callback rather than trying to pass multiple parameters back.

  • Add error handling in your callback function to check if response.responseXML exists before trying to read the answer attribute.

  • Never put GlideRecord queries or server-side API calls directly in client scripts — they'll either fail silently or cause security errors.

  • Group related functions in the same Script Include rather than creating a separate Script Include for every AJAX call — it's easier to maintain and performs better.

Test Your Knowledge

Quick 3-question quiz — see how your ServiceNow skills stack up.

Question 1 of 3Performance

A list view on a table with millions of records is slow. Best fix?

Select an answer to continue