- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2024 04:30 AM - edited 08-04-2024 04:30 AM
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:
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.
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.
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:
- Navigate to: System Definition > Script Includes.
- 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:
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2024 07:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2025 02:33 AM - edited 05-29-2025 02:34 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2024 05:44 AM
Hi @BillMartin
You do have some good information in your post. However, there are also some incorrect points in it as well.
@BillMartin wrote:
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.
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2024 07:25 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2025 02:33 AM - edited 05-29-2025 02:34 AM
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.