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 ,
A Script Include in ServiceNow is a reusable piece of server-side JavaScript code. Instead of copying and pasting the same logic across multiple Business Rules, UI Actions, or Workflows, you centralize it inside a Script Include and call it wherever it is needed.
There are 3 types :
1) Creating new class
2)Extending existing class i.e GlideAjax
3)On demand/classless
It provides methods such as addParam(), getXML(), and getXMLAnswer() to send parameters and retrieve responses asynchronously. A Script Include stores reusable server-side JavaScript logic and can be called from Client Scripts, Business Rules, and other server-side scripts. For client-side GlideAjax requests, we typically extend AbstractAjaxProcessor in a client-callable Script Include. A callback function processes the server response when it arrives. For example, when the Caller field changes on an Incident form, GlideAjax can call a Script Include to retrieve the caller's department and display it on the form. Unlike Script Includes, Business Rules execute automatically when their configured database conditions are met.
For more details and example refer this :
If this helps you then mark it as helpful and accept as solution.
Regards,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10 hours ago
Hi @sanketkhot7 ,
Kindly go through this Scripting in ServiceNow Fundamentals (Australia) leanring
Regards
BK
LinkedIn: https://www.linkedin.com/in/bhavyashree-karanth-55032a121
YouTube: https://www.youtube.com/@NowWithBhavya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10 hours ago - last edited 9 hours ago
Hi @sanketkhot7 ,
- GlideAjax:GlideAjax is the standard ServiceNow client-side API used to execute server-side code within a Script Include and pass the results back to the browser asynchronously.
- addParam():addParam() is used to call the function and pass the data from a client script to a server-side script.
- getXML():getXML() retrieves the full XML document.
- getXMLAnswer():getXMLAnswer()retrieves the raw string data.
- Script Include: A Script Include is a reusable server-side script. It can be called in a Client Script using GlideAjax, and used in a Business Rule by instantiating it as an object.
- AbstractAjaxProcessor: AbstractAjaxProcessor acts as the bridge between the browser and the database.
- GlideAjax: JavaScript function that processes the server-side response asynchronously after a request is sent from the client-side.
- Synchronous vs. Asynchronous: Synchronous and Asynchronous execution dictates whether a script waits for a process to finish before moving on, or lets it run in the background.
- Parameters: parameters are variables used to pass data between scripts.
- Business Rule vs. Script Include:
- Business Rules are triggers that automatically execute server-side logic when a record is created, updated, or deleted.
- Script Includes are reusable Functions that store server-side code to be called on-demand by other scripts.
- Practical Example:
- onChange Client Script():
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if (!newValue) {
g_form.clearValue('u_caller_department');
return;
}
var ga = new GlideAjax('CallerDepartmentAjax');
ga.addParam('sysparm_name', 'getDepartment');
ga.addParam('sysparm_caller_id', newValue);
ga.getXMLAnswer(function(answer) {
g_form.setValue('u_caller_department', answer || '');});
} - Script Include:
var CallerDepartmentAjax = Class.create();
CallerDepartmentAjax.prototype = Object.extendsObject(
AbstractAjaxProcessor, {getDepartment: function() {
var callerId = this.getParameter('sysparm_caller_id');if (!callerId) {
return '';
}var userGR = new GlideRecord('sys_user');
if (userGR.get(callerId)) {
var department = userGR.getValue('department');return department || '';
}return '';
},type: 'CallerDepartmentAjax'
}
);
- onChange Client Script():
Regards,
Tarun
