0% found this document useful (0 votes)
217 views7 pages

ServiceNow Script Include Overview

Script include in ServiceNow allows for reusable server-side JavaScript logic. It can contain functions, classes, and be called from other server-side scripts like business rules. GlideAjax allows calling script includes from the client-side and its getXMLAnswer method calls the processor asynchronously and returns only the answer element of the response.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 views7 pages

ServiceNow Script Include Overview

Script include in ServiceNow allows for reusable server-side JavaScript logic. It can contain functions, classes, and be called from other server-side scripts like business rules. GlideAjax allows calling script includes from the client-side and its getXMLAnswer method calls the processor asynchronously and returns only the answer element of the response.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction to Script Includes
  • Defining New Classes
  • Extending Classes in ServiceNow
  • Utilizing Script Include in Business Rule
  • Script Include Execution Context
  • Function Initialization
  • Methods of GlideAjax
  • Evaluating GlideAjax Efficiency

1.

Why do we use script Include


Answer- Script include in ServiceNow is used to perform server-side scripting in ServiceNow.
We can write java script functions and classes in script include. We can use those java script
functions and classes written in script include is another server-side script such as business rule,
script action etc.

It Is actually a reusable script logic which will only execute when called by other scripts such as
business rule, script action, client script in servicenow (through glide ajax) etc.

2. Different types of script include

Answer- There are different Script Includes types in ServiceNow:

On demand/classless:
On demand script include in ServiceNow does not contain class (class less) and it only defines a
single function. We cannot use on demand script include client side, so checking client callable
checkbox in script include interface is useless.

On demand script include can be defined as mentioned below:

On demand script include can be called in another server side scripts as mentioned below:
Extend an existing class:
Extending of class is basically inheriting of class, means accessing the fields and methods of
another class. We can extend script include class in ServiceNow as classes are extensible.
The mostly used extensible class is ‘AbstractAjaxProcessor’ (glide ajax – through which client
script communicate with server-side script).

We can extend another script include in an existing class as mentioned below:

Define a new class:


Creating a new script include is actually creating a new class. When we define script include in
ServiceNow, then basic syntax gets auto populated as mentioned below:

3. What are Different types of functions we can define in Script Include?


Answer- There are two types of function in script include, but these two types are only coming
into picture when client callable checkbox is check while creating the script include. Type of
functions as mentioned below.

Private function:
Private function in script include is basically which is not accessible from client side. Private
functions should have prefix “_”. Below is the screenshot for the same:

Public function:
Public function can be defined as mentioned below:

4. How to Call Script include in Business Rule?


Answer- script include in ServiceNow is a reusable script and can be accessible in any
ServiceNow server-side script.
So, we can call script include in business rule as mentioned below:
5. How to call ServiceNow Script include from client side?
Answer- To use script include servicenow at client side we have to make sure that client callable
checkbox in script include interface should be checked and script include class should be
extending class ‘AbstractAjaxProcessor’ (glide ajax – through which client script communicate
with server-side script).
Please find the below screenshot for the same:

6. When does the initialize () function of script include get executed?


Answer- When we are writing code only for server side means client checkbox is not checked
then we have a initialize function available in script include Class code.
This initialize function is actually very useful. Whatever code we write in initialize () function
then it will get execute every time when we create an object of script include class.
The initialize() function is basically the constructor for the initialized object.

var objDemo=new DemoScriptIncludeClass();

once we have initialized object objDemo of class DemoScriptIncludeClass as mentioned above


then the code written in initialize () will get executed.
I hope this helps to understand the concept.

7. What is [Link]() in script include and how we can use it?


Answer- If we want to call another script include within existing script include or current script
include then we use [Link](‘another script include name’).
This is actually very helpful and avoid writing same code again and again in every script include.
Other than copying the functions into your current script include you can call them directly in
existing script include using [Link](). For example:

Let’s say we have script include name FirstScriptInclude, where we have functions to add the
number. Now we are creating another script include name SecondScriptInclude, where we do
multiplications of the added numbers. Then what we will do, we will call script include Add in
Multiplication using code mentioned below through which we also have an access of add
functions.

[Link](‘FirstScriptInclude’)

You can also call one script include in another script as mentioned below

var objdemo=new FirstScriptInclude ();

8. How to call one function of script include to another function of same script include?
Answer- We can call internal function in the same script include as mentioned below:
[Link]();

If you need more information on any of the concepts then express your thoughts in below
comments box.

9. What is GlideAjax?
Answer- Glide ajax is a class used by client-side script. It allows the execution of server-side
code from client side We should initialize the glide ajax with script include name as a parameter
which we want to use.

Parameter sysparm_name is used to find the function of script include we want to execute.
When we have to give custom parameter then we should remember that it should start with
sysparm
For example: [Link]('sysparam_assignedTo' , emp);

10. What are the methods of GlideAjax?


Answer-
a. getXML();

getXML() is the preferred method for executing the code, because it is asynchronous and does
not hold up the execution of other client code.

b. getXMLWait();

getXMLWait(), is also available but is not recommended. Using getXMLWait() ensures the order
of execution, but can cause the application to seem unresponsive, significantly degrading the
user experience of any application that uses it. getXMLWait() is not available to scoped
applications.

c. getXMLAnswer();
Calls the processor asynchronously and gets the answer element of the response in XML
format. getXMLAnswer is slightly easier to write and getXML returns a whole XML Document
while getXMLAnswer only returns an Answer which is far more efficient.
getXMLAnswer returns the Answer and that's it. getXML still the Answer needs to be parsed.
getXMLAnswer is far more efficient looking at it this way.

Note: Actually we would change "GlideAjax" into "GlideAjax (with getXMLAnswer)". GlideAjax
with getXML still would return a whole document. While GlideAjax with getXMLAnswer would
only return the exact answer you need.

Common questions

Powered by AI

Choosing getXML() over getXMLAnswer() in GlideAjax methods can negatively impact performance due to increased computational overhead. While getXML() returns a whole XML Document that requires additional parsing, getXMLAnswer() only retrieves the necessary Answer element from the response, making it more efficient. The getXMLAnswer method thus reduces client-side processing time and memory usage, crucially improving the responsiveness and user experience of applications .

To call a function from another function within the same script include, the 'this' keyword followed by the function name is used. The syntax is 'this.functionName();'. This approach ensures that functions within the same script include can be modularly defined and accessed easily .

Script includes in ServiceNow are primarily used to perform server-side scripting more efficiently by allowing the reuse of code through JavaScript functions and classes. These reusable script logics can be called from other scripts such as business rules, script actions, and client scripts via GlideAjax. This reuse minimizes code duplication and enhances maintainability of the codebase, as changes can be made in one place rather than in multiple scripts .

The 'initialize()' function in a script include serves as a constructor for the class. It is executed every time an object of the script include class is created, housing any code meant to be run at that time. This makes the 'initialize()' function essential for setting up the initial state of an object. It is only executed when the class is used server-side without the client callable checkbox checked .

Creating reusable script logic through script includes enhances the maintainability of a ServiceNow application by centralizing code that performs common tasks. This centralization means changes are made in one place rather than spread across multiple scripts, reducing potential errors and lowering the effort required for updates. Such an approach also helps in debugging and testing as all logic is contained within well-defined units, promoting consistency and quality in codebase management .

Extending an existing class in a script include allows you to inherit the fields and methods of another class, which supports code reuse and inheritance, a key concept in object-oriented programming. This is particularly useful with classes like 'AbstractAjaxProcessor' for server-client communication through GlideAjax. On the other hand, defining a new class allows you to create a custom script include with specific functionality tailored to new requirements. Each method has its advantages: extending makes it easier to leverage existing logic, while a new class provides fresh functionality development .

Using getXMLWait() ensures execution order but can cause the application to seem unresponsive by holding up client code execution. This method leads to a degraded user experience due to potential UI freezing or delays. Compared to asynchronous methods such as getXML() or getXMLAnswer(), getXMLWait() increases the risk of extended load times and is not available in scoped applications, thus limiting its utility in modern applications .

The 'client callable' checkbox is crucial when designing script includes for client-side access because checking it allows the script include to extend 'AbstractAjaxProcessor', enabling GlideAjax communication between client scripts and server-side code. This ensures that client-side scripts can execute server-side logic effectively when needed. Failing to use this checkbox appropriately may lead to inaccessible server-side functions from the client, impeding the interactive functionality of the application .

Using gs.include() is preferable when you wish to leverage functionality from other script includes within a current script include without directly copying the code. This approach helps avoid code redundancy and promotes reusable code architecture. For example, complex calculations or business logic functions defined in one script include can be reused in others using gs.include(), thus introducing efficiency in maintaining and updating logic as changes only need to be made once .

Unchecked script includes in ServiceNow can significantly limit client-side functionality, as they prevent script includes from being called from client scripts. Without the 'client callable' option checked, scripts cannot extend 'AbstractAjaxProcessor', which is necessary for GlideAjax communication. This would restrict the ability to perform server-side logic on demand from the client side, thus hampering the dynamic interaction capabilities of the application .

1. Why do we use script Include 
Answer- Script include in ServiceNow is used to perform server-side scripting in ServiceNow.
Extend an existing class: 
Extending of class is basically inheriting of class, means accessing the fields and methods of
Answer- There are two types of function in script include, but these two types are only coming 
into picture when client call
5. How to call ServiceNow Script include from client side? 
Answer- To use script include servicenow at client side we
This initialize function is actually very useful. Whatever code we write in initialize () function 
then it will get execute
Answer- Glide ajax is a class used by client-side script. It allows the execution of server-side 
code from client side We sh
getXMLAnswer returns the Answer and that's it. getXML still the Answer needs to be parsed. 
getXMLAnswer is far more effici

You might also like