[Article] Script Includes in ServiceNow Explained: The Essential Guide for Developers and Admins
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Script includes often confuse new ServiceNow developers. Yet, understanding them unlocks a new level of efficiency and clean code. This post breaks down what a script include is, how to make one, and the power it brings to workflow automation. We’ll walk through a hands-on demo, highlight practical uses, and set you up to dig deeper into ServiceNow development.
If you want your ServiceNow code to be cleaner, easier to maintain, and future-proof, this guide is for you.
What is a Script Include in ServiceNow?
A script include is a reusable set of server-side JavaScript in ServiceNow. Think of it as a toolbox that holds useful tools (functions or methods) for your applications. Instead of writing the same code again and again in various places, you write it once in a script include and call it wherever you need.
This central approach means:
- No repeated code: You keep code dry (Don’t Repeat Yourself).
- Single place to update: Change your logic in one place and it updates everywhere.
- Consistent methods: Use the same, tested functions across different modules.
ServiceNow works as a Platform-as-a-Service (PaaS). This allows you to create and use script includes across various workflows and applications. You manage script includes from the System UI > Script Includes menu. Out of the box, ServiceNow includes many script includes, each built to direct core platform behaviors and manage workflows (like ITSM).
Script includes can be called by many parts of the system. When you set their accessibility to global, they’re available across different scope applications, making them a backbone of reusable logic.
Script Includes vs. Classes and Objects
If you know other programming languages, script includes are very similar to classes or objects.
- Let you bundle up related functions and properties.
- Encapsulate reusable code.
- Can be instantiated and called wherever you need.
- Make your code modular and easy to read.
In short: script includes are the classes and objects of ServiceNow.
How to Find and Create Script Includes in ServiceNow
Finding Script Includes in the Application Navigator
To get started, open the Application Navigator and type “script include” in the search box. You’ll see the path: System UI > Script Includes. Click there to view existing script includes.
As a system admin, you’ll have access to see, create, and manage all script include modules and features. This includes both custom ones and those pre-built by ServiceNow.
Creating Your First Script Include: A Step-by-Step Guide
Here’s how to create a new script include:
- Click "New" in the Script Includes list.
- Give it a clear name. A good naming convention is “HelloWorldUtils” if you’re making helper functions.
- Use “Utils” or “Utilities” as a suffix for collections of helper methods.
- Describe its purpose. Fill out the Description field, letting other developers know what it does.
- Set scope and accessibility.
- For highest reusability, pick “All application scopes.”
- You can restrict use to a single app if needed for security.
- Write your server-side JavaScript. The code pane comes with a default template and the initialize function.
- Save your work. Right-click the header and click Save, or use the Save button.
The ServiceNow code editor includes syntax highlighting, auto-complete, and detects issues in real time (like missing commas or typos).
Example: Creating a Simple HelloWorld Script Include
Imagine you want a function that says hello to whoever calls it. The script might look like this:
var HelloWorldUtils = Class.create(); HelloWorldUtils.prototype = { initialize: function() { }, sayHello: function(name) { return "Hello, " + name + ", from script include"; } };
- The sayHello method takes a name parameter and returns a greeting.
- Place any functions you want to reuse in this script include.
- Always check for red squiggly lines for syntax errors as you type.
Key tip: Use explicit, meaningful names for both your script includes and their methods.
Testing Script Includes: Calling and Using Them in ServiceNow
How You Can Call Script Includes
Script includes, being server-side JavaScript, can be called in several contexts:
- From other script includes (chaining logic).
- In business rules (server-automation triggers).
- GlideAjax (lets client scripts send parameters to, and receive responses from, server scripts).
- Directly via Scripts Background for testing and development.
Quick Table: Script Include Invocation Methods
Caller Type Use Case Example
Script Include | Reuse functions between classes | Data formatting utilities |
Business Rule | Server-side logic on record changes | Auto-assign on incident |
GlideAjax | Bridging client script to server for complex processing | Fetch user info dynamically |
Scripts Background | Test code without interfering with UI | Try out bulk updates |
Demo: Test Your Script Include with Scripts Background
Let’s see our HelloWorldUtils script include in action using the Scripts Background module. This tool runs server-side JavaScript right away, outside any UI context—a quick way to test your logic.
Steps to Call the Script Include
Go to the Scripts Background module.
Type your test code—be exact with names and case.
Example test:
var helloWorld = new HelloWorldUtils(); var message = helloWorld.sayHello('service developer'); gs.info(message);
Click Run Script.
Check the output at the bottom of the screen or in the log.
If everything is set, you’ll see:
Hello, service developer, from script include
- The script include did its job: took the name you passed and gave a personalized message.
- By using Scripts Background, you can quickly check if your server logic is working as expected before setting up more complex integrations.
Why This Matters
Script includes let you centralize your logic and share it between applications and modules. Change your business logic in a script include, and every part of your instance using it gets the update. This keeps your system easier to maintain, safer to refactor, and more reliable.
Advanced Uses and Best Practices for Script Includes
Accessibility and Scoping
Getting accessibility right is crucial. If you select All application scopes, your script include becomes callable from any app in your ServiceNow instance. This supports maximum reuse. If you only want it available within one application, select that scope.
Beware: Scope mismatches can block usage where you expect it. Always double-check your script include’s accessibility setting, especially in large or complex environments.
Using GlideAjax for Client-to-Server Calls
Want to call a script include from a client script on a ServiceNow form or UI? GlideAjax is your bridge. GlideAjax lets client scripts send requests to a server-side script include, get a response, and use that result in the UI.
- GlideAjax is essential for real-time data fetches and client/server communication.
- You’ll find many use cases: validating input, fetching related records, or running complex checks.
For a full walk-through of GlideAjax and client-callable script includes, check out the detailed lessons in the
ServiceNow developer tutorial playlist.
Tips for Clean, Safe Script Includes
- Use clear naming: Make your script include names unique and descriptive.
- Encapsulate logic: Don’t overstuff one script include. Use several small ones for better maintenance.
- Restrict accessibility where possible for security.
- Comment liberally: Help other developers (and future you) understand purpose and usage.
- Version your logic: Save often, and use comments to indicate changes.
T
ake Your ServiceNow Skills Even Further
Script includes are just one pillar of efficient ServiceNow development. As you master them, explore more tools to expand your skills:
- Business Rules: Automate server-side actions on record insert, update, or deletes.
- Client Scripts: Enhance UI interactions dynamically.
- Background Scripts: Test and bulk-edit records quickly.
- GlideAjax: Make your forms smarter by connecting client and server code.
Ready for deeper knowledge? Visit the full ServiceNow developer tutorial playlist for step-by-step guidance, practical demos, and best practices.
You can also support the channel and unlock more content by joining as a YouTube member.
And if you want to keep up with the latest tutorials, don't forget to subscribe and turn on notifications.
For a closer look at related ServiceNow automation and scripting, check out my walkthrough on automating approval workflows in ServiceNow.
Wrapping Up
Script includes help you build reusable, clean, and reliable solutions in ServiceNow. They keep your custom logic organized and enable you to serve multiple workflows without copying code. You’ll use them often, whether you’re tweaking ITSM modules, building new apps, or connecting to external systems.
Practice by starting with small, focused script includes. Test them in Scripts Background, then start linking them into your processes. As your skills grow, you’ll find script includes make every project easier to manage.
If you’re serious about leveling up your ServiceNow expertise, dive into the complete ServiceNow developer course playlist for in-depth lessons and live demos.
Thanks for reading. Keep learning, keep sharing, and happy developing!