ServiceNow Script Include-2
ServiceNow Script Include-2
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.
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 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).
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:
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
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);
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.