Script Includes
Script Includes in ServiceNow are reusable server-side JavaScript functions or classes that can be called from other server-side scripts, such as Business Rules, Scheduled Jobs, UI Actions, and other Script Includes. They serve as a foundational building block for application development and automa… Script Includes in ServiceNow are reusable server-side JavaScript functions or classes that can be called from other server-side scripts, such as Business Rules, Scheduled Jobs, UI Actions, and other Script Includes. They serve as a foundational building block for application development and automation within the ServiceNow platform. Script Includes are stored in the 'sys_script_include' table and are loaded on demand rather than being executed automatically. This makes them highly efficient, as they only consume resources when explicitly invoked. Each Script Include has a name, API name, and a script body that typically defines a class using the standard JavaScript prototype pattern. There are several types of Script Includes: 1. **On-Demand/Classless**: Simple functions that execute specific logic when called. 2. **Class-Based**: Object-oriented structures using prototypes, allowing initialization via constructors and method definitions. These extend the 'AbstractAjaxProcessor' or other base classes. 3. **Client-Callable**: Script Includes that extend 'AbstractAjaxProcessor' and have the 'Client Callable' checkbox enabled, allowing them to be invoked from client-side scripts via GlideAjax. Key features include: - **Reusability**: Write once, use across multiple scripts and applications. - **Encapsulation**: Bundle related logic into cohesive classes or functions. - **Scope Management**: Script Includes respect application scoping, ensuring proper access control in scoped applications. - **Extensibility**: Classes can extend other Script Includes, promoting inheritance and code modularity. For the Certified Application Developer exam, understanding Script Includes is critical. You should know how to create them, invoke them from Business Rules and client scripts (via GlideAjax), manage scope access, and apply best practices such as avoiding hardcoded values, including proper error handling, and leveraging the 'initialize' method for constructors. In application automation, Script Includes enable developers to centralize complex business logic, making workflows, integrations, and automated processes cleaner and more maintainable. They are essential for building scalable, enterprise-grade applications on the ServiceNow platform.
Script Includes in ServiceNow: A Complete Guide for CAD Exam Preparation
Script Includes in ServiceNow
Why Script Includes Are Important
Script Includes are one of the most fundamental and powerful server-side scripting mechanisms in ServiceNow. They are important because they promote code reusability, modularity, and maintainability across the platform. Without Script Includes, developers would need to duplicate logic across multiple Business Rules, Scheduled Jobs, and other server-side scripts, leading to inconsistency and difficulty in maintenance. Script Includes allow you to write a function or class once and call it from anywhere on the server side, making your code cleaner, more organized, and easier to debug.
For the ServiceNow Certified Application Developer (CAD) exam, Script Includes are a critical topic under the Application Automation domain. You will be expected to understand when to use them, how to create them, how they differ from other scripting mechanisms, and how they interact with client-side code.
What Is a Script Include?
A Script Include is a server-side reusable script that is stored in the Script Includes table (sys_script_include) in ServiceNow. It defines a JavaScript class or function that can be invoked from other server-side scripts such as Business Rules, Scheduled Jobs, UI Actions, Workflow Activities, Fix Scripts, and other Script Includes.
Key characteristics of Script Includes:
- They run on the server side only (unless made client-callable).
- They are only loaded when called, which makes them more efficient than global Business Rules that run on every transaction.
- They support object-oriented programming through JavaScript class definitions using the Class.create() pattern.
- They can be made client-callable by checking the "Client callable" checkbox, which allows them to be invoked from client-side scripts using GlideAjax.
- They are scoped to an application and respect application scope access controls.
How Script Includes Work
Script Includes follow a specific pattern in ServiceNow. Here is a breakdown of how they work:
1. The Class Structure
When you create a new Script Include, ServiceNow auto-generates a class template using the Class.create() pattern:
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = {
initialize: function() {
},
myMethod: function() {
// Your logic here
},
type: 'MyScriptInclude'
};
The initialize function acts as the constructor and is called when the class is instantiated. The type property must match the name of the Script Include.
2. Calling a Script Include from Server-Side Code
To use a Script Include from a Business Rule or another server-side script, you simply instantiate the class and call its methods:
var obj = new MyScriptInclude();
var result = obj.myMethod();
3. Client-Callable Script Includes (GlideAjax)
If you need to call a Script Include from the client side (e.g., from a Client Script or UI Policy), you must:
- Check the "Client callable" checkbox on the Script Include form.
- Extend AbstractAjaxProcessor instead of using the default class pattern.
- Use GlideAjax on the client side to invoke it asynchronously.
Server-side (Script Include):
var MyAjaxUtil = Class.create();
MyAjaxUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getMyData: function() {
var param = this.getParameter('sysparm_my_param');
// Process and return data
return 'Result: ' + param;
},
type: 'MyAjaxUtil'
});
Client-side (Client Script):
var ga = new GlideAjax('MyAjaxUtil');
ga.addParam('sysparm_name', 'getMyData');
ga.addParam('sysparm_my_param', 'Hello');
ga.getXMLAnswer(function(answer) {
alert(answer);
});
Key points about GlideAjax:
- sysparm_name specifies which method to call in the Script Include.
- Custom parameters use a sysparm_ prefix.
- The call is asynchronous by default (and asynchronous is the recommended approach).
- The Script Include must extend AbstractAjaxProcessor.
4. Extending Script Includes
Script Includes support inheritance. You can extend an existing Script Include using Object.extendsObject():
var ChildClass = Class.create();
ChildClass.prototype = Object.extendsObject(ParentClass, {
childMethod: function() {
// Child-specific logic
},
type: 'ChildClass'
});
This is how client-callable Script Includes extend AbstractAjaxProcessor, and it is also how you can create your own class hierarchies for complex applications.
5. Script Include Fields
Important fields on the Script Include form:
- Name: The name of the Script Include (must match the class name and the type property).
- API Name: The scoped name (e.g., x_myapp_0.MyScriptInclude) used when calling across scopes.
- Client callable: Checkbox that determines if the Script Include can be called via GlideAjax.
- Application: The application scope the Script Include belongs to.
- Accessible from: Controls cross-scope access (All application scopes, This application scope only).
- Active: Whether the Script Include is active.
- Script: The actual JavaScript code.
Script Includes vs. Other Scripting Mechanisms
Understanding the differences is critical for the exam:
- Script Include vs. Business Rule: Business Rules are triggered by database operations (insert, update, delete, query) on specific tables. Script Includes are not triggered automatically — they must be explicitly called. Use Script Includes for reusable logic; use Business Rules for table-specific event-driven logic.
- Script Include vs. UI Script: UI Scripts are client-side scripts that load on every page (global) or within specific forms. Script Includes are server-side. They serve completely different purposes.
- Script Include vs. Global Business Rule: Global Business Rules (no table specified) load on every server-side transaction, which is very inefficient. Script Includes only load when explicitly called, making them the preferred alternative to Global Business Rules.
Best Practices for Script Includes
- Always use Script Includes instead of Global Business Rules for reusable server-side logic.
- Keep the Script Include name, class name, and type property consistent and identical.
- Use descriptive method names that clearly indicate what the function does.
- Only check "Client callable" when the Script Include genuinely needs to be accessed from client-side scripts.
- When creating client-callable Script Includes, always extend AbstractAjaxProcessor.
- Use the initialize function for any setup logic that should execute upon instantiation.
- Document your Script Includes with comments explaining parameters, return values, and purpose.
- Respect application scoping — use the API Name when calling Script Includes across scopes.
Common Use Cases
- Utility functions used across multiple Business Rules (e.g., date calculations, string formatting).
- Encapsulating complex business logic that needs to be called from workflows, Business Rules, and UI Actions.
- Providing server-side data to client-side scripts via GlideAjax.
- Extending out-of-the-box ServiceNow classes to customize platform behavior.
- Creating reusable API layers for custom applications.
Exam Tips: Answering Questions on Script Includes
Tip 1: Know the GlideAjax Pattern Cold
The CAD exam frequently tests your understanding of how client-side scripts communicate with server-side Script Includes. Memorize the GlideAjax pattern: creating the GlideAjax object with the Script Include name, adding sysparm_name to specify the method, adding custom parameters with sysparm_ prefix, and using getXMLAnswer() for asynchronous callbacks. Also remember that the Script Include must extend AbstractAjaxProcessor and have the Client callable checkbox enabled.
Tip 2: Remember That Script Includes Are On-Demand
A key differentiator tested on the exam is that Script Includes are only loaded when explicitly called. This is what makes them superior to Global Business Rules. If a question asks about the best way to create reusable server-side logic, the answer is almost always Script Include.
Tip 3: Watch for the "Client callable" Trap
Exam questions may present scenarios where a Script Include is being called via GlideAjax but is not working. The most common reason is that the Client callable checkbox is not checked, or the Script Include does not extend AbstractAjaxProcessor. Look for these details in the answer choices.
Tip 4: Understand the Naming Convention
The Script Include Name, the class variable name, and the type property must all match. Exam questions may test this by showing code where these values are mismatched and asking you to identify the error.
Tip 5: Know When NOT to Use a Script Include
If a question describes logic that should run automatically when a record is inserted or updated, that is a Business Rule, not a Script Include. Script Includes do not trigger on database events. However, a Business Rule might call a Script Include to execute reusable logic.
Tip 6: Understand Scope and Access
In scoped application development, Script Includes respect application scope. Know that the Accessible from field controls whether other application scopes can call the Script Include. When calling a Script Include from another scope, you must use the API Name (which includes the scope prefix).
Tip 7: Recognize the AbstractAjaxProcessor Methods
For client-callable Script Includes, know the key methods: this.getParameter('sysparm_param_name') to retrieve parameters passed from the client, and the return value of the function is what gets sent back to the client via getXMLAnswer(). Some questions may also reference this.newItem('element_name') for returning XML, but getXMLAnswer with a simple return value is the most commonly tested pattern.
Tip 8: Distinguish Synchronous vs. Asynchronous GlideAjax Calls
The exam may test whether you know that asynchronous GlideAjax calls (using getXMLAnswer with a callback function) are the recommended approach. Synchronous calls (getXMLWait()) block the user interface and are not recommended. If a question asks for the best practice, always choose the asynchronous option.
Tip 9: Elimination Strategy
When facing a question about server-side reusable code, eliminate answers that suggest: client-side scripts (wrong execution context), Global Business Rules (inefficient and deprecated practice), or UI Policies (client-side, field-level behavior). The correct answer for reusable server-side logic is consistently Script Includes.
Tip 10: Practice Scenario-Based Questions
The CAD exam often presents real-world scenarios. For example: "A developer needs to create a reusable function that calculates SLA breach time and needs to call it from multiple Business Rules and a Scheduled Job. What should the developer use?" The answer is a Script Include. Practice identifying these patterns — whenever the question emphasizes reusability and server-side execution, think Script Include.
🎓 Unlock Premium Access
ServiceNow Certified Application Developer + ALL Certifications
- 🎓 Access to ALL Certifications: Study for any certification on our platform with one subscription
- 3305 Superior-grade ServiceNow Certified Application Developer practice questions
- Unlimited practice tests across all certifications
- Detailed explanations for every question
- CAD: 5 full exams plus all other certification exams
- 100% Satisfaction Guaranteed: Full refund if unsatisfied
- Risk-Free: 7-day free trial with all premium features!