The CreatorCon Call for Content is officially open! Get started here.

๐Ÿ“ฆ Mastering Script Include in ServiceNow: Types, Best Practices, and Real-World Use Cases

Bhavesh Patil
Tera Contributor

Hey #ServiceNowCommunity ๐Ÿ‘‹

Have you ever written logic that you needed to reuse across multiple scripts in ServiceNow?

Thatโ€™s where Script Includes come inโ€”an essential tool for writing modular, reusable, and secure server-side code.

In this post, letโ€™s dive into everything about Script Includes: their types, real-time use cases, best practices, and some pro tips you don't want to miss!


๐Ÿ’กWhat is a Script Include?

A Script Include in ServiceNow is a reusable server-side script that defines a function or class which can be called from other server scripts (Business Rules, Scheduled Jobs, Flow Actions, etc.), or even client-side (if exposed as callable via glideAjax).

It helps you avoid code duplication, improve maintainability, and enable separation of concerns in complex logic.


๐Ÿง  Importance of Script Includes

โœ…Centralizes reusable logic
โœ…Improves script efficiency & readability
โœ…Reduces maintenance cost
โœ…Supports object-oriented scripting in ServiceNow
โœ…Enables server-client communication (via GlideAjax)


๐Ÿงฉ Types of Script Includes (Explained Deeply with Examples)

Script Includes can be categorized based on how theyโ€™re used and accessed.


๐Ÿ”น1. On demand/classless :- 

  • A Script Include that defines a single function is known as on demand script include, It does not contain class hence it is also called as classless Script Include.
  • This script include is callable from only other server-side scripts.
  • We cannot use On demand script include in client side even if the Client callable checkbox is check or not.
  • The Script Include name and function name should be exactly same.

๐Ÿ› Use Case: Reusable utility methods for date formatting, string manipulation, etc.

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

    getFormattedDate: function () {
        return new GlideDateTime().getDisplayValue();
    },

    type: 'CommonUtils'
};

// Call from Business Rule:
var utils = new CommonUtils();
var date = utils.getFormattedDate();

๐Ÿ”น2. Extend an existing class :-

Client Callable Script Include (GlideAjax)

  • Extend an existing class is basically inherit the other class, means accessing the fields and methods of other classes. & this is the only type which work server to client side.
  • In this type of script include we can create multiple function as well.
  • The mostly used extensible class is โ€˜AbstractAjaxProcessorโ€™ so that client script communicate with server-side script.

๐Ÿ› Use Case: Fetch user details, approval status, or server-side values on form load.

// Script Include
var SIname = Class.create();
SIname.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getInfo: function () {
        var sysID = this.getParameter('sysparm_sysID');
        var obj = {};

        var grUser = new GlideRecord('sys_user');
        if (grUser.get(sysID)) {
            obj.DepartmentName = grUser.department.name.toString();
        }

        return JSON.stringify(obj);
    },

    type: 'SIname'
});

 

// Client Script
var callerSysID = g_form.getValue('u_caller');

var ga = new GlideAjax('SIname');
ga.addParam('sysparm_name', 'getInfo');
ga.addParam('sysparm_sysID', callerSysID);
ga.getXMLAnswer(function(response) {
    var parsedData = JSON.parse(response);
    g_form.setValue('department', parsedData.DepartmentName);
});

๐Ÿ”น3.  Define a new class :-  

Script Include with Prototype Methods (OOP Style)

  • When we create a new script include that is actually Define a new class. It is also callable from server side. we cannot call it on client side.
  • In this type of script include we can create multiple functions & this is the difference between Define a new class & On demand script include.

๐Ÿ› Use Case: Class with business logic that handles complex operations.

var TaskManager = Class.create();
TaskManager.prototype = {
    initialize: function(taskSysId) {
        this.task = new GlideRecord('task');
        this.task.get(taskSysId);
    },

    isOverdue: function () {
        return this.task.due_date < new GlideDateTime();
    },

    assignToCurrentUser: function () {
        this.task.assigned_to = gs.getUserID();
        this.task.update();
    },

    type: 'TaskManager'
};

โš”๏ธDifference Between Script Include Types

FeatureOn DemandExtend and Existing Define New class
Accessed FromServer-side onlyClient-side (via GlideAjax)Server-side
Client Callable BoxโŒUncheckedโœ…CheckedโŒOptional
Use CaseUtility logicFetch data for clientBusiness logic as object
Return TypeAny dataString (for getXMLAnswer)Any

๐Ÿง  Best Practices for Script Includes

โœ…Keep each Script Include focused on a single responsibility
โœ…Use meaningful names (e.g., IncidentHelper, ApprovalUtil)
โœ…Use gs.info() for logging and debugging, remove after deployment
โœ…Avoid hardcoding valuesโ€”use gs.getProperty() for dynamic config
โœ…Return only whatโ€™s needed; reduce payload for client-callable scripts
โœ…Mark Script Includes as Client Callable only if absolutely needed
โœ…Document methods with comments and parameter expectations
โœ…Use Flow Designer Script Actions instead of custom scripting when possible for simpler logic


๐Ÿ’ผReal-Time Use Cases of Script Includes

  1. ๐Ÿ”Reusable approval logic shared across multiple flows and BRs

  2. ๐Ÿ“ฉSend formatted email notifications based on templates and record types

  3. ๐Ÿ”Check user roles or permissions before executing server-side logic

  4. ๐Ÿ”„Call external APIs (via RESTMessageV2) with reusable code blocks

  5. ๐Ÿ“ฅFetch data from parent-child records and return values to the client side


๐Ÿ’กPro Tips

๐Ÿ”นUse Script Includes with Flow Designer Script Actions to bring reusability into no/low-code environments.

๐Ÿ”นDonโ€™t expose sensitive logic in Client Callable scripts. Always validate user permissions on the server-side.

๐Ÿ”นUse JSUtil.nil() instead of == null or == '' to safely check for null/empty values in ServiceNow.

๐Ÿ”นGroup related functions under one class rather than creating many tiny Script Includes.


๐ŸFinal Thoughts

Script Includes are one of the most powerful yet often underutilized features in ServiceNow. Whether you're optimizing background logic or enabling dynamic client-side behaviors, a well-structured Script Include makes your code modular, clean, and scalable.

Have a unique use case or challenge with Script Includes? Letโ€™s discuss in the comments below! ๐Ÿ’ฌ

**Please mark it as *Helpful* or *Correct* โ€” it really means a lot!**

2 REPLIES 2