Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
232 views

ServiceNow Script Include-2

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
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views

ServiceNow Script Include-2

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
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 gs.include() 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 gs.include(‘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 gs.include(). 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.

gs.include(‘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:
this.functionName();

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: ga.addParam('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.

You might also like