Script Includes

Create a Script Include in ServiceNow

Script Includes let you write server-side JavaScript functions once and call them from anywhere in your ServiceNow instance. This guide shows you how to build both simple function-style Script Includes and class-based ones that can be instantiated.

Why Script Includes exist

Before Script Includes, developers copied the same JavaScript logic across multiple Business Rules, Client Scripts, and Scheduled Jobs. When that shared logic needed updating, you'd hunt through dozens of scripts trying to find every copy. Script Includes solve this by giving you a single place to write server-side functions that can be called from anywhere. Platform admins and developers use them to centralize validation logic, API integrations, complex calculations, and utility functions that multiple parts of the system need.

How Script Includes work

A Script Include is JavaScript that runs on the server and gets cached in memory for performance. You can write them as simple functions (good for utilities like string formatting or date calculations) or as classes with properties and methods (better for complex objects like API clients or data processors). The key decision is accessibility: 'This application scope only' restricts usage to your current scope, while 'Accessible from all application scopes' makes it available everywhere. Most custom Script Includes should be scoped to avoid namespace pollution.

Making them production-ready

Basic Script Includes work, but production ones include error handling, input validation, and proper documentation. Add JSDoc comments so other developers understand the parameters and return values. For class-based Script Includes, implement consistent initialization patterns and expose only the methods that external code should call. The biggest enhancement is making them callable from client-side scripts using GlideAjax — this lets you run server-side logic from Client Scripts without violating security restrictions.

Before you start

  • admin role or script_include_admin role
Sourdough
Chrome Extension

Sourdough: ServiceNow Monitoring and Analytics

A Chrome extension for ServiceNow Admins and Developers with essential tools, analytics, graphs and monitoring features.

Instance HealthGraphs & ChartsAPI HealthDeveloper ToolsQuick SearchInstance Switcher
Add to Chrome

Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.

Overview
Tasks
CMDB
API
Metrics
Monitor
Internals
Instance:sourdoughdev·Version:Yokohama
Instance StateONLINE
System StatusFully Operational
Session Timeout90 minutes
Logged-In Sessions2 (20 active)
Build Nameyokohama-12-18-2024_p1
IP Address10.159.128.43
Instance HealthHealth Score: 90%
🔥 5dSourdough (Chrome Plugin)Dark Mode

Step by step

1

Navigate to Script Includes

Go to System Definition > Script Includes in the navigator. Click New to create a new Script Include record.

TIP

Use the filter navigator and type 'script includes' — it's faster than drilling through menus.

2

Set basic Script Include properties

Enter a descriptive Name that follows your naming conventions — this becomes the function or class name you'll call. Set Accessible from to 'This application scope only' unless you specifically need it available globally. Leave Active checked and Client callable unchecked for now.

TIP

The Name field must match exactly what you'll type in your calling code — capitalization matters.

3

Write a simple function-style Script Include

In the Script field, write a basic function. For example: `function formatPhoneNumber(phone) { if (!phone) return ''; return phone.replace(/\D/g, '').replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3'); }` This creates a reusable utility function that other scripts can call directly.

TIP

Function-style Script Includes are perfect for utilities that don't need to maintain state between calls.

4

Create a class-based Script Include

For more complex logic, use a class structure instead. Replace the function with: `var UserHelper = Class.create(); UserHelper.prototype = { initialize: function() {}, getActiveUserCount: function() { var gr = new GlideRecord('sys_user'); gr.addActiveQuery(); gr.query(); return gr.getRowCount(); }, type: 'UserHelper' };` Save the record.

TIP

Class-based Script Includes require the `new` operator when called: `var helper = new UserHelper();`

5

Test the Script Include

Open Scripts - Background from the navigator. For function-style, test with: `gs.info(formatPhoneNumber('1234567890'));` For class-based, test with: `var helper = new UserHelper(); gs.info(helper.getActiveUserCount());` Run the script to verify it works.

TIP

Always test Script Includes in Background Scripts before using them in production Business Rules.

6

Make it client-callable via GlideAjax

To call your Script Include from client scripts, check 'Client callable' on the Script Include record and extend AbstractAjaxProcessor instead of Class.create(). Modify your class: `var UserHelper = Class.create(); UserHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, { getActiveUserCount: function() { // your logic here return this.newItem('count', userCount); }, type: 'UserHelper' });`

TIP

Client-callable Script Includes must return data using `this.newItem(key, value)` methods.

7

Call from other scripts

In Business Rules or other server-side scripts, call function-style Script Includes directly: `formatPhoneNumber(current.phone)`. For classes, instantiate first: `var helper = new UserHelper(); helper.getActiveUserCount();` From client scripts, use GlideAjax if the Script Include is client-callable.

TIP

Server-side scripts can call Script Includes directly — GlideAjax is only needed from client-side code.

Best practices

  • Always validate input parameters at the start of your functions — Script Includes get called from many places with varying data quality.

  • Use descriptive names and add JSDoc comments describing parameters and return values — other developers will thank you when they inherit your code.

  • Don't make Script Includes client-callable unless you specifically need to call them from client scripts — it adds overhead and potential security exposure.

  • Keep Script Includes focused on a single responsibility — a Script Include that 'does everything' becomes impossible to maintain and debug.

  • Test Script Includes thoroughly in Background Scripts before deploying them — a broken Script Include can cascade failures across multiple Business Rules and workflows.

Test Your Knowledge

Quick 3-question quiz — see how your ServiceNow skills stack up.

Question 1 of 3Performance

A list view on a table with millions of records is slow. Best fix?

Select an answer to continue