Script Include

sanketkhot7
Tera Contributor

What is Script Include In detail? and their types with scenarios use cases

7 REPLIES 7

Mark Manders
Tera Patron

Google!!!

And please let us know the types of script includes you find. 

Roshan Singh 07
Tera Expert

RoshanSingh07_0-1793284514182.png

 

glideFather
Tera Patron

ahoy @sanketkhot7,

 

your question is very basic and perhaps could be easily answered, however you can try this:

ServiceNow Application Developer  >> Server-side Scripting >> Script Includes

 

glideFather_0-1793290340688.png

 

yashkamde
Giga Sage

Hello @sanketkhot7 ,

 

A Script Include is a reusable script that defines functions or classes. Instead of duplicating the same code across multiple Business Rules, Workflows, UI Actions, or Client Scripts you write the logic once inside a Script Include and call it wherever it is needed.

 

Generally developers classify Script Includes into three types :

 

1) On-Demand / Classless :
This type contains just a single function rather than an entire object class.

// Example :
function validateEmailAddress(emailStr) {
    var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(emailStr);
}

// How to call
var isValid = validateEmailAddress('test@example.com');

 

2) Defining a New Class :

This creates a custom object with its own properties and multiple methods.

// Example 
var SI_Name= Class.create();
SI_Name.prototype = {
    initialize: function() {},

    isStockAvailable: function(modelId) {
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('model', modelId);
        gr.addQuery('install_status', '6'); // In Stock
        gr.query();
        return gr.hasNext();
    },

    type: 'SI_Name'
};

// Call It
var assetHelper = new SI_Name();
var available = assetHelper.isStockAvailable('sys_id');

 

3) Extending an Existing Class :

This allows you to inherit all the behaviors of an existing class and override or add new capabilities.
Also very common use case in ServiceNow is extending the ootb AbstractAjaxProcessor class to make the Script Include Client-Callable via GlideAjax.

// Example: Extending AbstractAjaxProcessor to make it Client-Callable
var MyCustomUser = Class.create();
MyCustomUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    // method to check if user is VIP
    getUserVIPStatus: function() {
        var userId = this.getParameter('sysparm_user_id');
        var gr = new GlideRecord('sys_user');
        if (gr.get(userId)) {
            return gr.getValue('vip');
        }
        return 'false';
    },

    type: 'MyCustomUser'
});

// How to call (Client Script using GlideAjax)
var ga = new GlideAjax('MyCustomUser');
ga.addParam('sysparm_name', 'getUserVIPStatus');
ga.addParam('sysparm_user_id', g_form.getValue('caller_id'));
ga.getXMLAnswer(function(answer) {
    alert('Is Caller VIP? ' + answer);
});

 

If my response helped mark as helpful and accept the solution.