ServiceNow Scripting Best Practices: Three Key Concepts of Script Includes (With Sample Use Cases)

BillMartin
Mega Sage

In this article, I would like to share the key concepts of Script Includes. Beginner or experienced Implementors and Developers will benefit from the example use cases I have provided.

 

Let us drill down!

 

In ServiceNow, Script Includes are reusable server-side scripts that can be used across the platform to define functions or classes that you want to call from other scripts, business rules, or scripts in other modules. They help keep your code modular and maintainable. Here’s an overview of how Script Includes work and some examples to get you started.

 

Three Key Concepts of Scripts Include:

  1. Global vs. Scoped:

    • Global Script Includes can be accessed from any application within the instance.
    • Scoped Script Includes are specific to the application they are defined in and can only be accessed from within that application.
  2. Client-side vs. Server-side:

    • Script Includes are generally used on the server-side. If you need to use functions in client scripts, you will need to make them accessible through an AJAX call or similar mechanism.
  3. Instantiation:

    • Script Includes can be instantiated as a class or used as a singleton. Singleton Script Includes provide a way to create global functions that can be called without instantiating the class.

How to Create a Script Include:

To create a Script Include in ServiceNow:

  1. Navigate to: System Definition > Script Includes.
  2. Click: New to create a new Script Include.

Here’s a basic example of a Script Include that defines a class with a method:

 

var MyScriptInclude = Class.create();
MyScriptInclude.prototype = {
initialize: function() {
// Initialization code if needed
},

myMethod: function(param) {
// Your server-side logic here
return "Hello, " + param;
},

type: 'MyScriptInclude'
};

 

Scripts Include Example Use Cases:

  1. Utility Functions:

    Suppose you need a utility function to format dates:

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

formatDate: function(date) {
if (!date) return '';
var formattedDate = gs.dateGenerate(date, 'MM/dd/yyyy');
return formattedDate;
},

type: 'DateUtils'
};

 

You can call DateUtils.formatDate() from other scripts.

 

2. Business Logic:

 

If you need to implement some business logic, like calculating discounts:

 

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

calculateDiscount: function(amount, discountRate) {
if (amount <= 0 || discountRate < 0) return 0;
return amount * (discountRate / 100);
},

type: 'DiscountCalculator'
};

 

How to Call Script Includes

To call a Script Include from another server-side script, you can do something like this:

 

var discountCalc = new DiscountCalculator();
var discount = discountCalc.calculateDiscount(100, 15);
gs.info('Discount amount: ' + discount);

 

For client-side calls, you’ll typically use an AJAX call to invoke server-side functions exposed by Script Includes.

 

Scripts Include Best Practices

  • Modularity: Keep your Script Includes focused on a single responsibility or a closely related set of functions.
  • Documentation: Comment your code well, especially if the Script Include will be used by others.
  • Testing: Test Script Includes thoroughly to ensure they work as expected across different scenarios.

By using Script Includes, you ensure that your code is organized, reusable, and easier to maintain, which is especially valuable in large ServiceNow instances.

 

Here is How: ServiceNow Scripting: When to Use Script Includes

 

Please mark as helpful if you find the article lucrative.

2 ACCEPTED SOLUTIONS

BillMartin
Mega Sage

Thanks @ChrisBurks for improving the article!

View solution in original post

BillMartin
Mega Sage

hello all,

 

I’ve created a dedicated YouTube playlist designed to help you fully understand how to build Script Includes the right way — using object-oriented programming (OOP) principles and proven software architecture patterns. This series will guide you step-by-step through writing clean, reusable, and scalable code in ServiceNow.

 

View solution in original post

3 REPLIES 3

ChrisBurks
Mega Sage

Hi @BillMartin 

 

You do have some good information in your post. However, there are also some incorrect points in it as well.

 


@BillMartin wrote:

 

  1. Global vs. Scoped:

    • Global Script Includes can be accessed from any application within the instance.
    • Scoped Script Includes are specific to the application they are defined in and can only be accessed from within that application.

 

These two points are not correct. There is a field named "Accessible from" on the Script Include record that allows the the Script Include to be accessible from only the same scope or from any other scope.

scope_accessibility.png

 

In the screenshot one can see that it is true for even Script Includes in the Global scope.

 

In order to access Script Includes from different scopes use the API namespace when calling a Script Include. Reference the field named "API name" on the Script Include.

 

 

 

 

//example calling script include w/ global namespace

var si = new global.MyScriptInclude();

//example calling script include in the Human Resources Core scope

var si = new sn_hr_core.hr_CaseUtils();

 

 

 

Script Include API Name:

api_name_script_include.png

Sometimes a Restricted Caller Access record may be needed however, the point is that a script include can be locked down to only being accessible from a particular scope or can be open from any scope whether it's a Global or Scoped Script Include. 

 

Reference: https://docs.servicenow.com/bundle/xanadu-api-reference/page/script/server-scripting/concept/c_Scrip...

 

BillMartin
Mega Sage

Thanks @ChrisBurks for improving the article!

BillMartin
Mega Sage

hello all,

 

I’ve created a dedicated YouTube playlist designed to help you fully understand how to build Script Includes the right way — using object-oriented programming (OOP) principles and proven software architecture patterns. This series will guide you step-by-step through writing clean, reusable, and scalable code in ServiceNow.