GlideAjax and Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi everyone,
I want to understand GlideAjax and Script Includes in ServiceNow from basics to advanced level.
Could someone please explain the following concepts with real-time examples and step-by-step code explanations?
GlideAjax: What is GlideAjax, why do we use it, and what are its methods, such as addParam(), getXML(), and getXMLAnswer()?
Script Include: What is a Script Include, why was it introduced when we already have Business Rules, and what are its types and methods?
AbstractAjaxProcessor: What is it, why do we extend it in a client-callable Script Include, and how does it work?
Callback Function: What is a callback function, why is it required with asynchronous GlideAjax calls, and how does it work?
Synchronous vs. Asynchronous: What is the difference, and how does asynchronous execution work in GlideAjax?
Parameters: What is the purpose of sysparm_name, sysparm_type, sysparm_value, and custom parameters such as sysparm_caller_id? How do we retrieve them using this.getParameter()?
Execution Flow: How does a request travel from a Client Script to GlideAjax, then to a Script Include, and finally return a response to the Client Script?
Business Rule vs. Script Include: What is the difference, when should we use each, and can a Script Include be called from a Business Rule or other server-side scripts?
Practical Example: Please explain a real-time scenario, such as retrieving a caller's department when the Caller field changes on an Incident form, with complete code for both the Client Script and Script Include.
I want to understand not only the syntax but also why, when, and how each method is used, with simple explanations and practical examples.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello @sanketkhot7 ,
Let's understand GlideAjax and Script Includes in simple terms, using a real-time ServiceNow example.
1. What is a Script Include?
A Script Include is reusable JavaScript code that runs on the server.
For example, if you need to retrieve a caller's department from the User table, you can write that logic in a Script Include and reuse it whenever required.
2. What is GlideAjax?
GlideAjax allows a Client Script to call a client-callable Script Include on the server and receive a response without refreshing the form.
Execution flow:
Agent selects Caller → Client Script executes → GlideAjax calls Script Include → Server retrieves the department → Response returns to the Client Script → Department field gets populated.
3. Practical example: Populate the caller's department
Step 1: Create a Script Include
Navigate to System Definition → Script Includes.
Name: CallerDetailsAjax
Client callable: Checked
Extends: global.AbstractAjaxProcessor
var CallerDetailsAjax = Class.create();
CallerDetailsAjax.prototype = Object.extendsObject(
global.AbstractAjaxProcessor, {
getDepartment: function() {
var callerId = this.getParameter('sysparm_caller_id');
var userGR = new GlideRecord('sys_user');
if (userGR.get(callerId)) {
return userGR.getDisplayValue('department');
}
return '';
},
type: 'CallerDetailsAjax'
}
);Step 2: Create an onChange Client Script
Table: Incident
Field name: Caller
Type: onChange
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return;
}
var ga = new GlideAjax('CallerDetailsAjax');
ga.addParam('sysparm_name', 'getDepartment');
ga.addParam('sysparm_caller_id', newValue);
ga.getXMLAnswer(function(answer) {
g_form.setValue('u_caller_department', answer);
});
}Replace u_caller_department with your actual target field name.
4. How does the code work?
GlideAjax('CallerDetailsAjax') identifies the Script Include.
addParam('sysparm_name', 'getDepartment') specifies the server-side function to execute.
sysparm_caller_id passes the selected caller's sys_id.
this.getParameter() reads the parameter on the server.
GlideRecord retrieves the user record.
getXMLAnswer() receives the returned value through a callback.
g_form.setValue() populates the target field.
5. Important differences
Script Include: Stores reusable server-side logic.
GlideAjax: Enables client-to-server calls for client-callable Script Includes.
Business Rule: Executes server-side logic based on configured record events and conditions.
Best practice: Use asynchronous methods such as getXMLAnswer() instead of getXMLWait(), and validate inputs and access permissions before returning sensitive information.
6. Additional references and learning resources
Official ServiceNow documentation
ServiceNow Community article
YouTube learning resources
Use the official documentation to validate API behavior and the practical examples to understand implementation. Please verify video availability and publication dates before sharing, as these can change.
If you find this helpful , Accept the solution and mark Helpful .
Thankyou
Yamsani Bhavani
ServiceNow Developer - SecOps, GRC, Custom Applications, Now assist
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
hi @sanketkhot7 ,
Let’s understand GlideAjax and Script Includes with a practical example.
1. What is GlideAjax?
GlideAjax allows a Client Script to call a server-side Script Include and retrieve data without reloading the form.
For example, when you select a Caller on an Incident form, you want to retrieve their department automatically. Instead of reloading the form, you can use GlideAjax to get the department from the server.
2. What is a Script Include?
A Script Include is a reusable server-side JavaScript component. You can write logic once and call it from Business Rules, other server-side scripts, or Client Scripts through GlideAjax when configured as client-callable.
Common types include:
-
On-demand/Classless: A function or script that can be called directly, depending on its configuration.
-
Class-based: A reusable class created using
Class.create(). -
Client-callable: A Script Include that can receive requests from client-side GlideAjax.
3. What is AbstractAjaxProcessor?
AbstractAjaxProcessor is the base class used by client-callable Script Includes to handle GlideAjax requests.
When extending it, you can retrieve client parameters using this.getParameter() and return a response to the Client Script.
4. What are addParam(), getXMLAnswer(), and callbacks?
-
addParam()sends a parameter to the server. -
getXMLAnswer()sends the request asynchronously and provides the returned answer to a callback function. -
getXML()also sends an asynchronous request, but the callback receives the XML response object.
A callback function runs when the server response becomes available. This allows the form to remain responsive while the request is processed.
5. Practical example: Get the Caller’s department
Step 1: Create a Script Include
-
Name:
CallerDetailsAjax -
Select Client callable.
-
Extend
global.AbstractAjaxProcessorif your Script Include is in a scoped application.
Example:
var CallerDetailsAjax = Class.create();
CallerDetailsAjax.prototype = Object.extendsObject(
global.AbstractAjaxProcessor, {
getDepartment: function() {
var callerId = this.getParameter('sysparm_caller_id');
if (!callerId)
return '';
var user = new GlideRecord('sys_user');
if (user.get(callerId))
return user.getValue('department') || '';
return '';
},
type: 'CallerDetailsAjax'
}
);
Step 2: Create an onChange Client Script
-
Table: Incident
-
Field: Caller
-
Type: onChange
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue)
return;
var ga = new GlideAjax('CallerDetailsAjax');
ga.addParam('sysparm_name', 'getDepartment');
ga.addParam('sysparm_caller_id', newValue);
ga.getXMLAnswer(function(answer) {
if (answer) {
g_form.setValue('u_caller_department', answer);
} else {
g_form.clearValue('u_caller_department');
}
});
}
Replace u_caller_department with the actual field name on your Incident form.
6. How does the request work?
-
The user changes the Caller field.
-
The Client Script creates a GlideAjax request.
-
sysparm_nametells ServiceNow which Script Include method to execute. -
sysparm_caller_idsends the selected user's sys_id. -
The Script Include retrieves the department.
-
The server returns the department's sys_id.
-
The callback receives the answer and updates the form field.
7. What is the difference between a Business Rule and a Script Include?
A Business Rule runs automatically when its configured database operation or condition occurs. A Script Include contains reusable logic that other scripts can call.
For example, you can create a Script Include to calculate an employee's eligibility and call it from multiple Business Rules instead of duplicating the same code.
8. What are the parameters?
-
sysparm_name: The method to execute. -
sysparm_type: An optional parameter used by some implementations; it has no universal special meaning. -
sysparm_value: A custom parameter commonly used to pass a value. -
sysparm_caller_id: A custom parameter containing the selected caller's sys_id.
You retrieve custom parameters using:
var callerId = this.getParameter('sysparm_caller_id');
9. Synchronous vs. asynchronous
With asynchronous GlideAjax, ServiceNow sends the request and continues running other client-side code. The callback executes when the response arrives.
Avoid synchronous requests because they can block the browser while waiting for the server.
One important point: always validate input and enforce appropriate access controls on the server. Client-callable does not mean every user should be allowed to retrieve every piece of data.
If this helped, mark it helpful and accept the solution.
Regards,
Tejas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi @sanketkhot7
First Complete Scripting in ServiceNow Fundamentals (Australia) course from Servicenow University.
Module 7 depicts fundamental concepts of Script Include.
Regarding GlideAjax query - you will get answer of your queries from this Servicenow Documentation: GlideAjax - Client
Also check : https://www.youtube.com/watch?v=jhbF_qK0kZA
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello @sanketkhot7 ,
Basically, GlideAjax is the recommended ServiceNow API for calling server side logic (inside a Script Include) from a client-side script (like a Client Script or UI Policy) asynchronously without freezing the user's browser.
You can try to understand this from below image :
If my response helped mark as helpful and accept the solution.
