- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
17m ago
How do you call a ServiceNow Script Include from a Client Script? Use GlideAjax. This step-by-step example shows a client-callable Script Include that runs a GlideRecord query on the server and returns the incident caller's email, business phone, location and manager email to the Incident form as JSON. An onChange Client Script displays the values with g_form.showFieldMsg(). A full video demonstration is included.
Watch the video:
Free download: ServiceNow-Script-Include-GlideAjax-Example.zip (both scripts with setup steps)
What is a Script Include in ServiceNow?
A Script Include is reusable server-side JavaScript, usually a class or utility, saved once and called from anywhere on the platform: Business Rules, other scripts, flows and, when marked Client callable, Client Scripts. Reusability is the main reason to use one. Write the logic once instead of copying it into every script.
What is GlideAjax in ServiceNow?
GlideAjax is the client-side API that lets a Client Script call a client-callable Script Include on the server and get a response back asynchronously. It brings the data you need to the client in one trip, so the form doesn't keep calling the server.
Step 1: Create the client-callable Script Include
Go to System Definition > Script Includes > New.
- Name: GetCallerInfoForSC
- Client callable: checked. The class then extends AbstractAjaxProcessor.
var GetCallerInfoForSC = Class.create();
GetCallerInfoForSC.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCallerInfo: function() {
try {
var incidentCallerInformation = [];
var incNumber = this.getParameter('sysparm_incNumber').toString();
gs.info('inc number ' + incNumber);
if (!gs.nil(incNumber)) {
var incidentCaller = new GlideRecord('incident');
incidentCaller.addQuery('number', incNumber);
incidentCaller.query();
while (incidentCaller.next()) {
var callerRecord = {};
callerRecord.email = incidentCaller.caller_id.email.getDisplayValue();
callerRecord.businessPhone = incidentCaller.caller_id.phone.getDisplayValue();
callerRecord.location = incidentCaller.caller_id.location.full_name.getDisplayValue();
callerRecord.managerEmail = incidentCaller.caller_id.manager.email.getDisplayValue();
incidentCallerInformation.push(callerRecord);
}
return new JSON().encode(incidentCallerInformation);
} else {
return 'nothing is returned';
}
} catch (error) {
}
},
type: 'GetCallerInfoForSC'
});
Step 2: Create the onChange Client Script that calls GlideAjax
Go to System Definition > Client Scripts > New.
- Table: Incident
- Type: onChange
- Field name: Caller
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
alert('caller id ' + g_form.getValue('number'));
var callerInformation = new GlideAjax('GetCallerInfoForSC');
callerInformation.addParam('sysparm_name', 'getCallerInfo');
callerInformation.addParam('sysparm_incNumber', g_form.getValue('number'));
callerInformation.getXML(ajaxProcessor);
function ajaxProcessor(response) {
try {
var answer = response.responseXML.documentElement.getAttribute("answer");
var callerInfo = answer.evalJSON();
g_form.showFieldMsg('caller_id', 'Email: ' + callerInfo[0].email);
g_form.showFieldMsg('caller_id', 'Business Phone: ' + callerInfo[0].businessPhone);
g_form.showFieldMsg('caller_id', 'Location: ' + callerInfo[0].location);
g_form.showFieldMsg('caller_id', 'Manager Email: ' + callerInfo[0].managerEmail);
} catch (error) {
alert("error message " + error);
}
}
}
How the Client Script, GlideAjax and Script Include work together
- The Caller changes on the Incident form, and the onChange Client Script fires.
- GlideAjax calls the GetCallerInfoForSC Script Include. It passes the method name in sysparm_name and the incident number in sysparm_incNumber.
- The Script Include reads the parameter with this.getParameter() and runs a GlideRecord query on the incident table. It dot-walks to the caller's user record.
- It builds an array of objects and returns it as a JSON string.
- The Client Script reads the answer, parses the JSON and shows each value with g_form.showFieldMsg().
Why use GlideAjax instead of GlideRecord in a Client Script?
GlideRecord in a Client Script makes slow, synchronous server calls and isn't best practice. Run the GlideRecord query in a Script Include and call it from the Client Script with GlideAjax.
Why GlideAjax returns null: common fixes
- The Script Include name or function name in the GlideAjax call doesn't match exactly.
- Client callable isn't checked on the Script Include.
- The user doesn't meet the access control on the client-callable Script Include, or the call crosses an application scope.
- The caller's user record has no email, phone, location or manager.
FAQ
What is the difference between getXML and getXMLAnswer in GlideAjax?
getXML gives you the full XML response, and you read the answer attribute yourself, as this demo does. getXMLAnswer passes the answer string straight to your callback, so the code is shorter.
What does client callable mean on a ServiceNow Script Include?
It lets Client Scripts call the Script Include through GlideAjax. The class extends AbstractAjaxProcessor.
How do I pass parameters from GlideAjax to a Script Include?
Use addParam('sysparm_yourName', value) in the Client Script and this.getParameter('sysparm_yourName') in the Script Include.
How do I return multiple values from a Script Include to a Client Script?
Put them in an object or array, return it as JSON from the server, and parse it on the client.
Is Script Include and GlideAjax on the ServiceNow CAD exam?
Yes. Client Scripts, Script Includes, GlideAjax and server-side scripting are core topics for the ServiceNow Certified Application Developer (CAD) exam.
Tips from the demo
- Use alert() and gs.info() while you test, then remove them or cut them back before production.
- Always return something meaningful from the Script Include, even when the input is empty.
- Learn JavaScript basics first: arrays, objects, try/catch and JSON. They make ServiceNow scripting much easier.
Download the scripts
Get both scripts and the setup steps in one zip: ServiceNow-Script-Include-GlideAjax-Example.zip. You can also browse the code on GitHub at servicenow-script-include-glideajax.
This lesson is part of my free ServiceNow developer training on YouTube. The full course material is on GitHub: servicenow-developer-course.
If you have questions, or a different way you'd approach this, please share it in the comments.
Bill Martin
TechTalk with Bill