Script Includes
Introduction
A Script Include in ServiceNow is a reusable server-side script that is created in the Script Include
table. It allows the creation of server-side business logic that can be called by other scripts or
modules. Script Includes are primarily used to store reusable functions or methods that are needed
across various parts of the system.
Key Components of Script Includes
When creating or defining a Script Include, several key components are involved. These include:
1. Name: The unique name that identifies the Script Include within ServiceNow.
2. API Name: The name used for referencing the Script Include programmatically in other scripts.
3. Glide AJAX Enabled: Enables the Script Include to be called from client-side scripts using Glide
AJAX for server-client communication.
4. Mobile Callable: Allows the Script Include to be invoked from mobile interfaces in ServiceNow.
5. Sandbox Enabled: Enables testing the Script Include in a sandbox environment before deploying
to production.
6. Application: Specifies the ServiceNow application to which the Script Include belongs.
7. Accessible From: Defines the accessibility of the Script Include, either globally or within a specific
application scope.
8. Active: Indicates whether the Script Include is active and available for use.
9. Description: Provides a brief summary of what the Script Include does, for documentation
purposes.
Purpose and Benefits of Script Includes
Script Includes serve the purpose of encapsulating reusable business logic and making it available
across ServiceNow. The key benefits include:
1. Reusability: Once a Script Include is created, it can be invoked from anywhere in
ServiceNow, such as other scripts, business rules, UI actions, etc.
2. Maintainability: When you centralize commonly used scripts in Script Includes, it becomes
easier to maintain and update the code. You only need to make changes in one place.
3. Performance: Script Includes are efficient as they are executed on the server-side. This can
help offload tasks from the client and ensure faster performance.
Types of Script Includes
There are two main types of Script Includes in ServiceNow:
1. Global Script Includes: These are available globally and can be invoked from any part of
ServiceNow.
2. Scoped Script Includes: These are defined within a specific application scope and can only
be accessed by scripts within the same scope.
Syntax of Script Includes
A basic Script Include structure looks like this:
In the above example, a class called 'MyScriptInclude' is created using the Class.create() method. The
prototype of this class is defined, with methods such as 'initialize' and 'myFunction'. The 'type'
property is used to define the type of the Script Include.
How to Create a Script Include
To create a Script Include in ServiceNow, follow these steps:
1. Navigate to System Definition > Script Includes in the application navigator.
2. Click on New to create a new Script Include.
3. Fill in the required fields like Name, API Name, and Description.
4. Write the script logic in the script section.
5. Save or submit the Script Include.
Example of Using a Script Include
Here’s an example of calling a Script Include from a Business Rule:
In this example, we are instantiating the 'MyScriptInclude' class and calling its 'myFunction' method.
The result is then logged using gs.info().
Best Practices for Script Includes
When using Script Includes in ServiceNow, it's important to follow these best practices:
1. Use Naming Conventions: Follow a consistent naming convention for your Script Includes
so they are easy to identify and understand.
2. Avoid Hardcoding: Try to avoid hardcoding values in your Script Includes. Use variables or
configuration values that can be changed easily.
3. Modularize Your Code: Break down large scripts into smaller, reusable functions or
methods within the Script Include.