Script Includes

Create a GlideAjax-Callable Script Include

GlideAjax lets you run server-side logic from client scripts without forcing a page reload or form submission. This guide walks you through creating the Script Include and shows the client-side pattern to call it.

Why GlideAjax exists

Before GlideAjax, client scripts had no clean way to get server data or run server logic without submitting the form or redirecting. You'd see developers stuffing business logic into UI Policies, abusing reference qualifiers, or forcing unnecessary form submissions just to run a server calculation. Client script developers needed server data for field validation, dynamic dropdowns, or real-time lookups — but the platform gave them no direct path. The result was either clunky user experiences or fragile workarounds that broke when underlying data changed.

How GlideAjax Script Includes work

A GlideAjax Script Include extends AbstractAjaxProcessor and exposes specific functions to client scripts through HTTP requests that happen behind the scenes. The Script Include receives parameters from the client using getParameter(), runs whatever server logic you need, and sends data back using setAnswer() for simple values or JSON for complex objects. The 'Client callable' checkbox is what makes this work — it tells ServiceNow to expose your Script Include to the front-end. On the client side, you instantiate a GlideAjax object, pass parameters, and handle the response in a callback function.

Building production-quality AJAX calls

Basic GlideAjax works fine for simple lookups, but production implementations need error handling, input validation, and performance considerations. Add parameter validation in your Script Include before running queries — client data is never trustworthy. Structure your functions to return consistent JSON with status indicators so client scripts can handle success and failure states properly. For frequently-called functions, consider caching results or limiting database queries. The best GlideAjax implementations feel instantaneous to users and degrade gracefully when something goes wrong server-side.

Before you start

  • script_admin role or higher
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 record

Navigate to System Definition > Script Includes and click New. Set the Name field to something descriptive like 'MyAjaxUtils' — this becomes your client-side class name. Check the Client callable checkbox immediately — forgetting this is the most common mistake and your AJAX calls will fail silently without it.

TIP

Use a consistent naming convention like 'AjaxUtils' or 'ClientAPI' so other developers know which Script Includes are callable.

2

Set up the class structure

In the Script field, start with the basic structure: your Script Include must extend AbstractAjaxProcessor and include the type declaration. Use this template but replace 'MyAjaxUtils' with your actual Script Include name. The initialize function is required even if empty.

TIP

The class name in your script must exactly match the Name field or client calls won't work.

3

Add your callable function

Create a function that client scripts can call — this is your business logic. Use this.getParameter('parameterName') to receive data from the client side. The parameter name you use here must match what you send from the client script. Keep the function focused on one task for easier debugging.

TIP

Always validate parameters before using them — client scripts can send anything, including malicious data.

4

Return data to the client

Use this.setAnswer() for simple string values or return a JSON object for complex data. If using setAnswer(), the client receives whatever you pass as a string. For JSON, stringify your object and the client can parse it back. Don't mix both approaches in the same function.

TIP

JSON return values give you more flexibility and make error handling much cleaner on the client side.

5

Save and test the Script Include

Click Submit to save your Script Include. The system will validate your syntax — fix any errors before proceeding. Your Script Include is now available to client scripts, but you need to write the client-side code to call it.

TIP

Test server-side logic first by running your functions in a background script before writing client code.

6

Write the client-side GlideAjax call

In your client script, create a new GlideAjax object using your Script Include name, add parameters with addParam(), and call getXMLAnswer() with a callback function. The first parameter must always be 'sysparm_name' with your function name as the value. Handle the response in your callback — it receives the server response as a parameter.

TIP

Always check if the response is valid before using it — server errors or network issues can return unexpected values.

7

Add error handling

Wrap your client-side AJAX call in a try-catch block and add server-side validation in your Script Include. Check that required parameters exist and have valid values before running database queries. Return error messages as part of your JSON response so client scripts can display meaningful feedback to users.

Best practices

  • Always validate input parameters server-side before using them in queries — client scripts can send malicious or malformed data that breaks your logic or creates security vulnerabilities.

  • Use JSON returns instead of setAnswer() for anything more complex than a single string value — it makes error handling and response parsing much more reliable on the client side.

  • Keep GlideAjax functions focused on single tasks rather than building one massive function that handles multiple operations — debugging AJAX calls is hard enough without tangled logic paths.

  • Cache expensive database queries or calculations within your Script Include session rather than hitting the database on every AJAX call from the same user session.

  • Return consistent response structures that include success/failure indicators so client scripts can handle errors gracefully instead of breaking when something goes wrong server-side.

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