ServiceNow
Script Includes
Script Include stores reusable JavaScript for execution on the server. It cannot be run
automatically must be called to run. It can be client callable.
*NOTE: Backend name of Script Include table is sys_script_include.
Or You can navigate through Application Navigator [All > System UI > Script Includes].
Current Application Scope.
By Choosing All Application Scopes
Enter name of your Script means that this SI can only be
Include. called form this application scope.
This API Name
field will
automatically
By Choosing This Application
poplulated on the By checking this checkbox we can make Scopes only means that this SI can
basis of current this script Include client callable means be called form any application
scope & name of we can use it in client-side. scope.
Script Incude
[[Link]
e].With the help of
this API Name we
can call this Script Here we can write script.
Include.
When we give name to the Script Include it When we give name to the Script Include and
will autopopulate the script section like: check the client callable checkbox it will
autopopulate the script section like:
var Test = [Link](); var Test = [Link]();
[Link] = { [Link] =
initialize: function() { [Link](AbstractAjaxProcessor,
}, {
type: 'Test' type: 'Test'
}; });
JavaScript APIs:
Classes and Methods that can be used in script to define functionality of the platform.
Client-Side APIs Server-Side APIs
GlideRecord GlideRecord
GlideForm GlideAggeregate
GlideAjax GlideDateTime
GlideUser GlideSystem
GlideList GlideElement
Types of Script Includes:
• Classless/On-Demand
• Define a new Class
• Extend an existing Class
A) Classless/On Demand Script Include:
• In this type of Script Include we can only create a single function and the name of the Script
Include must be same as the function name.
• We have to delete the autopopulated script and directly mention the function in the script
section.
• This Script Include can only be called from Server-Side.
Name of Script Include and Function are same.
Use the Classless Script Include:
• Simply call the function from any server-Side Script.
• Most Script Includes, when called server-side, are called from Business Rules.
• To use a Classless Script Include from a Business Rule, simply reference the function.
Directly call the fucntion but only from Server side scripts.
B) Define a New Class:
• Stores multiple functions.
• After the record is named, a Script Editor Macro is inserted in the Script field to help guide
you.
• Script Include name MUST be an EXACT match to the Class name.
Define a function:
• Each function definition begins with
the function name followed by a colon.
• Then the function keyword and
parentheses.
• Function arguments/parameters may be
added inside the parentheses as per any
function definition.
• Write the script to execute between the
curly braces
Calling Script Include in Client Side:
• For Calling the Script
Include we use
GlideAjax Api.
GlideAjax API:
• It is Client-Side Api used to get Server-Side data into Client-Side.
• This can be useful in situations where the client script needs to retrieve data from the server,
perform calculations or other processing on the server, or otherwise interact with the
server-side data or systems.
• It is important to use GlideAjax as making too many requests can impact performance.
Method Name Description
new Creates a new GlideAjax instance.
GlideAjax(scriptIncludeName) The scriptIncludeName parameter specifies the name of the
Script Include that contains the server-side code to be run.
addParam(name, value) Adds a parameter to the GlideAjax request. The name and
value parameters specify the name and value of the
parameter.
getXML(responseHandler) GetXML is an async method which is used to get the data
from the script include in the client script, after receiving
the data we have to parse the data into xml format using:
“[Link]”.
getXMLAnswer() GetXMLAnswer is also an async method used to get the
data from the script include but here we do not have to
parse the data into XML format.
getXMLWait() GetXMLWait was a synchronous method which means it
{Deprecated (Not in use Now)} holds the user session up until the response is received from
the function call, which results in a bad user experience.
Global Variables in a Script Include:
Declared once at the beginning of the script in the initialize() function and are available to every
function in the Class.
C) Extend an Existing Class:
In this type of Script Include we write the new functionalities of the script and reference an
Existing class using the extendsObject() method.
This is the example of Extend a Script Include from any other class.