This article is a supplement to the ServiceNow documentation. For full documentation please refer ServiceNow official website
Checkout our NEW Video Channel you can like and subscribe too!

Introduction:

Probably one of the most important concepts of ServiceNow for a developer to learn is Script Include. ServiceNow comes with preloaded tons of script include that solves specific problem for the module. Script Include help to promote reusability , modularize components and efficiency of client server model in ServiceNow. But before we start we need to Know Glide Ajax.

Glide Ajax

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

Glide Ajax are two types

Asynchronous It doesn’t wait for the response from the server. It uses call back function getXml() Synchronous function is used for synchronous Glide Ajax call getXmWaitl()

Always validate, that client callable check box is checked in the script include which is used in Glide Ajax(client side)

addParam() method is used with the parameters to execute the script include method and is used to provide values.

// Here initialize the Glide Ajax with parameter as script include name (here: classdemo)
var ga = new GlideAjax('classdemo');

// Here pass the function name  “getManagerName” as parameter which is part of the script include
ga.addParam('sysparm_name', 'getManagerName');

// Here we pass the additional variable

ga.addParam('sysparam_assignedTo', g_form.getValue('assigned_to'));

// Asynchronous Glide Ajax calls the call back function.

ga.getXML(classdemo);

// Body of the call backfunction	
function classdemo(response) {

    // Here we get the XML response and parse the answer attribute.

    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('u_manager_name', answer);
}

Script Include

Script include is used to store Java script that runs on the server or script Includes are reusable Java script definition which can be called from any server-side script and in some cases from the client side too.It is used in business Rule, Reference qualifier, UI action and in other script Include.

While creating script include  we should make sure that names of the script include prototype and type should be the same.

Script include can be Public or private. Most client callable script include are marked private by default. Also important to note that Script include is execute only when explicitly called by other scripts.

There are three different type of script includes

  • On demand/classless
  • Extend an existing class
  • Define a new class

Extend an existing class

var Classdemo = Class.create();
Classdemo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    type: 'Classdemo'
});

Define a new class

var Classdemo = Class.create();
Classdemo.prototype = {
    initialize: function() {},

    type: 'Classdemo'
};

Example:

Table:Incident

When we populate assigned to field , corresponding Manager name will get auto-populated in a custom field Manager Name.

scriptinclude231120191.PNG

Onchange client Script

scriptinclude231120192.PNG

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var emp = g_form.getValue('assigned_to');
    var ga = new GlideAjax('classdemo');
    ga.addParam('sysparm_name', 'getManagerName');
    ga.addParam('sysparam_assignedTo', emp);
    ga.getXML(classdemo);

    function classdemo(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_manager_name', answer);
    }
}

Script Include:

scriptinclude231120193.PNG

var classdemo = Class.create();
classdemo.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getManagerName: function() {
        var sysid = this.getParameter("sysparam_assignedTo");
        var managername = '';
        var empname = new GlideRecord('sys_user');
        empname.addQuery('sys_id', sysid);
        empname.query();
        if (empname.next()) {
            managername = empname.manager;
        }
        return managername.getDisplayValue();
    }
});
    Content