Script Includes are powerful server-side JavaScript functions in ServiceNow that provide reusable code libraries accessible throughout the platform. They serve as a centralized repository for custom functions and classes that can be called from various server-side scripts, including Business Rules,…Script Includes are powerful server-side JavaScript functions in ServiceNow that provide reusable code libraries accessible throughout the platform. They serve as a centralized repository for custom functions and classes that can be called from various server-side scripts, including Business Rules, Scheduled Jobs, UI Actions, and other Script Includes.
Key characteristics of Script Includes include:
1. **Reusability**: Instead of duplicating code across multiple scripts, you can write a function once in a Script Include and call it whenever needed. This promotes the DRY (Don't Repeat Yourself) principle and simplifies maintenance.
2. **On-Demand Loading**: Script Includes are loaded only when explicitly invoked, which improves system performance compared to having code execute on every transaction.
3. **Client Callable Option**: By checking the 'Client callable' checkbox, Script Includes can be accessed from client-side scripts using GlideAjax, enabling asynchronous server calls from the browser.
4. **API Access**: Script Includes have full access to server-side APIs like GlideRecord, GlideSystem (gs), and GlideDateTime, making them ideal for complex data operations.
5. **Extending Classes**: You can create Script Includes that extend the AbstractAjaxProcessor class for client-callable functionality or extend other custom classes for object-oriented programming approaches.
To create a Script Include, navigate to System Definition > Script Includes and click New. You must provide a name (which becomes the class name), specify the API name, and write your JavaScript code. The standard structure uses a prototype pattern with an initialize function and type property.
Best practices include using descriptive naming conventions, adding comprehensive comments, implementing proper error handling, and organizing related functions within logical Script Includes. Testing Script Includes can be done through the Scripts - Background module or by creating test Business Rules.
Script Includes are fundamental for ServiceNow development, enabling cleaner architecture, better code organization, and improved application maintainability across your instance.
Script Includes in ServiceNow
What are Script Includes?
Script Includes are reusable server-side JavaScript functions that can be called from other server-side scripts in ServiceNow. They act as a library of functions that can be invoked on demand, promoting code reusability and maintainability across the platform.
Why are Script Includes Important?
Script Includes are essential for several reasons:
• Code Reusability: Write once, use many times across Business Rules, Scheduled Jobs, UI Actions, and other server-side scripts • Better Performance: They only load when called, unlike global Business Rules that run on every transaction • Organized Code: Keeps your codebase clean and modular • Easier Maintenance: Update logic in one place instead of multiple scripts • Client-Callable Option: Can be accessed from client-side scripts using GlideAjax
How Script Includes Work
Script Includes use a class-based structure in JavaScript. Here's the typical structure:
var MyScriptInclude = Class.create(); MyScriptInclude.prototype = { initialize: function() { }, myFunction: function(parameter) { // Your logic here return result; }, type: 'MyScriptInclude' };
Key Properties of Script Includes:
• Name: Must match the class name in the script • API Name: Used to call the Script Include • Client Callable: Checkbox that enables GlideAjax access • Active: Determines if the Script Include is available • Accessible from: Controls scope access
Calling Script Includes
From Server-Side Scripts: var si = new MyScriptInclude(); var result = si.myFunction(parameter);
From Client-Side Scripts (using GlideAjax): The Script Include must be marked as Client Callable and extend AbstractAjaxProcessor.
Exam Tips: Answering Questions on Script Includes
1. Remember the naming convention: The Script Include name MUST match the class name defined in the script. This is a common exam question.
2. Know when to use them: Script Includes are ideal when you need the same logic in multiple places. If logic is only needed once, a Business Rule might be sufficient.
3. Understand Client Callable: For GlideAjax to work, the Script Include must: - Have 'Client Callable' checked - Extend AbstractAjaxProcessor - Use getParameter() to receive values - Return values properly
4. Know the difference from Business Rules: Script Includes are on-demand and must be explicitly called, while Business Rules run automatically based on conditions.
5. Scope awareness: Understand that Script Includes respect application scope. A Script Include in one scope may not be accessible from another unless properly configured.
6. Common exam scenarios: - When to choose Script Include over Business Rule - How to make a Script Include accessible to client scripts - Troubleshooting why a Script Include is not working (name mismatch, not active, scope issues)
7. The initialize function: This is the constructor that runs when you create a new instance. Use it to set up variables or configurations needed by other methods.
8. Performance consideration: Script Includes are more efficient than global Business Rules because they only execute when specifically called.