ServiceNow Script Includes

Script Includes are reusable JavaScript libraries that live on the ServiceNow server. Instead of writing the same logic in multiple Business Rules, Scheduled Jobs, or REST APIs, a developer writes the logic once in a Script Include and calls it from anywhere. Script Includes keep code organized, reduce duplication, and make maintenance far easier.

Why Script Includes Exist

Imagine writing a function that calculates a user's SLA eligibility. Without Script Includes, this function gets copy-pasted into three Business Rules, two Scheduled Jobs, and a REST API endpoint. When the business changes the eligibility rules, a developer must find and update all six copies — and likely misses one.

WITHOUT Script Include:        WITH Script Include:
Business Rule 1: [code]        Business Rule 1: SLAUtils.check()
Business Rule 2: [code]        Business Rule 2: SLAUtils.check()
Business Rule 3: [code]        Business Rule 3: SLAUtils.check()
Scheduled Job:   [code]        Scheduled Job:   SLAUtils.check()
REST API:        [code]        REST API:        SLAUtils.check()

Update needed: 5 places        Update needed: 1 place only

Accessing Script Includes

Navigate to System Definition > Script Includes in the left navigation. The list shows all Script Includes in the current scope. Click any entry to open and edit the JavaScript code. Click New to create a fresh one.

Anatomy of a Script Include

Every Script Include has these key fields:

  • Name: The class or function name used to call it from other scripts.
  • API Name: The full namespace reference (scope.Name).
  • Client callable: If checked, Client Scripts and GlideAjax calls can access this Script Include from the browser.
  • Script: The JavaScript code that defines the class and its methods.

Class-Based Script Include Structure

ServiceNow Script Includes follow a specific class pattern using the Class.create() structure. This pattern is the standard way to write reusable, object-oriented server-side code.

var IncidentUtils = Class.create();
IncidentUtils.prototype = {
    initialize: function() {
        // Constructor - runs when the class is instantiated
    },

    getOpenCount: function(groupName) {
        var count = new GlideAggregate('incident');
        count.addQuery('assignment_group.name', groupName);
        count.addQuery('state', 'NOT IN', '6,7');
        count.addAggregate('COUNT');
        count.query();
        if (count.next()) {
            return count.getAggregate('COUNT');
        }
        return 0;
    },

    type: 'IncidentUtils'
};

Calling the Script Include from a Business Rule

// Inside a Business Rule script:
var utils = new IncidentUtils();
var openCount = utils.getOpenCount('Network Support');
gs.log('Network Support has ' + openCount + ' open incidents.');

Static vs. Instance Methods

Script Includes support two calling styles:

Instance Method (most common)

Create an instance of the class first, then call methods on it. This is the pattern shown above — var utils = new IncidentUtils(); then utils.getOpenCount(...);.

Static Method

Call the method directly on the class without instantiating it. This works for utility functions that don't need state. Declare the method as a function at the top level of the Script Include rather than inside the prototype object.

// Static-style Script Include
var MathUtils = Class.create();
MathUtils = {
    roundToTwo: function(num) {
        return Math.round(num * 100) / 100;
    }
};

// Call directly without "new":
var result = MathUtils.roundToTwo(3.14159);

Application Scope and Script Includes

Script Includes live within an application scope. A Script Include in the global scope is accessible from any script in the instance. A Script Include in a custom application scope is only accessible from scripts within that same scope. Scoped Script Includes protect code from interference by other applications and are required in store apps published to the ServiceNow App Store.

Client-Callable Script Includes and GlideAjax

Checking the "Client callable" checkbox on a Script Include makes it accessible from browser-side scripts via GlideAjax. This is the only safe, supported way for a Client Script to fetch server-side data without a full page reload. The Script Include runs on the server, and GlideAjax brings the result back to the browser asynchronously. GlideAjax is covered in detail in its own dedicated topic.

Debugging Script Includes

Use gs.log() or gs.debug() statements inside Script Include methods to output values to the system log. Navigate to System Log > All to read these messages after triggering the code. The Script Debugger — accessible from any script editor — sets breakpoints and steps through execution line by line, making it possible to inspect variable values mid-execution without adding log statements.

Naming Conventions

Well-named Script Includes communicate their purpose at a glance. Use PascalCase for class names and descriptive suffixes that indicate the purpose:

  • IncidentUtils: Utility functions related to incidents
  • UserHelper: Helper methods for user-related operations
  • CatalogValidator: Validation logic for catalog items
  • SLACalculator: SLA computation methods

Clear naming means any developer reading a Business Rule script immediately understands what an external call does — without needing to open the Script Include itself.

Leave a Comment

Your email address will not be published. Required fields are marked *