How to Call Script Include in ServiceNow l Script Include Examples

Article provides complete understanding of Script Include ServiceNow with Examples. In this ServiceNow tutorial, we talk about each and every concepts of servicenow script include. That how to define script include in ServiceNow with examples, how to call script include in business rule, types of script include in ServiceNow, types of functions in script include, how to use script include from client-side scripts and lots of other things.

script include servicenow,servicenow script include,script include in servicenow,script inlcude glideajax example


I believe this article will help beginners to prepare answers of script include questions. This article is complete package to explore script include in ServiceNow in a single go.
So Let's Start..


What is script include ServiceNow?

Script include in ServiceNow is use 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. 

We can call script include function and classes anywhere while doing server-side scripting in ServiceNow. In below sections, we have mentioned the way that how we can call script include in other server-side scripts.


Types of functions in ServiceNow Script Include?

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:
script include servicenow,script include in servicenow,servicenow script include with example
On Demand Script Include in ServiceNow


On demand script include can be called in another server side scripts as mentioned below:
servicenow script include with example
On Demand Script Include in ServiceNow


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:
script include in servicenow,servicenow script include
Script Include in ServiceNow Extends


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:
script include servicenow,script include in servicenow with example
ServiceNow Script Include Example


Different types of functions we can define in Script Include?

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:
Define Private Function in Script Include
Define Private Function in Script Include


Public function: 

Public function can be defined as mentioned below:
Define Public Function in Script Include
Define Public Function in Script Include


How to Call Script include in Business Rule?

As we know from above definition that 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:


Call Script Include in Business Rule
Call Script Include in Business Rule


How to Use ServiceNow Script include client side?

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:

Client Callable Script Include in ServiceNow
Client Callable Script Include in ServiceNow

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

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.

What is gs.include() and how we can use it ?

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 ();

How to call one function of script include to another function of same script include?

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.


ServiceNow Script Include Video Tutorial:

Below script include video tutorial helps you to understand script include in servicenow with example.



I hope this servicenow tutorial provides you complete understanding of script include in servicenow and clear your all the doubts, you have in servicenow script include. If you like this Servicenow tutorial then please do share this with others from below social networks. Thank you !!!! Good Luck.

9 comments:

  1. This has really helped me out in understanding glide ajax and script includes for my new job.

    ReplyDelete
  2. this is helpful information. i am continuing to fail to get my callback to execute my script include. i have ensure the client callable is set, my script include is active, extends AbstractAjaxProcessor and the class name and type match the script include name. however, when i call from the glideajax, nothing logs in my script include and the client always returns: {"responseXML":{"location":null},"status":""}

    ReplyDelete
  3. very useful for servicenow developers

    ReplyDelete
  4. Yes, excellent information. I am a ServiceNow beginner in the development realm. Things were made very clear here!! Thanks!!

    ReplyDelete
  5. Hello Team



    I have to call one script include to another script include without creating the object with new Keyword.

    Suppose i have to:-



    1) first script include Named Test1:-

    function Test1()

    {
    gs.addInfoMessage(' called second script include');
    }



    2) Second Script Include Test2:-



    function Test2()
    {
    var a=10;
    var b=20;
    var c=a+b;
    gs.addInfoMessage(c);
    }



    Now I want to call Test2 into Test1 wihout creating Object with New keyword . I want to call Test2 into Test 1 Using gs.include() how we can call this .



    I Checked with gs.include('Test2') It not working in Business Rule Output is displaying for Test1..



    Please help me to resolve this .

    ReplyDelete
  6. Very Helpful. Thank you for making such nice article on Script Include topic.

    ReplyDelete

Thankyou !!!!

Powered by Blogger.