Class-based Script Includes give you proper object-oriented utilities that other scripts can instantiate and use. You'll build a reusable class with multiple methods using ServiceNow's Class.create() pattern.
Why class-based Script Includes exist
Most Script Includes start as simple utility functions — getApprovalCount(), updateRelatedRecords(), calculateSomething(). That works fine until you need related functionality scattered across multiple functions that share state or configuration. You end up with a dozen loosely-related functions, repeated parameter passing, and no clean way to maintain state between calls. Developers building complex integrations, custom applications, or sophisticated business logic run into this wall constantly.
How ServiceNow's class pattern works
ServiceNow uses a specific Class.create() pattern for object-oriented JavaScript that predates modern ES6 classes. You define a constructor (initialize method), add methods to the prototype, and instantiate with new ClassName(). This gives you proper encapsulation — each instance maintains its own state, you can pass configuration during instantiation, and methods can call each other cleanly. The pattern looks different from modern JavaScript but it's what ServiceNow's platform expects and what performs well in their Rhino engine.
Building production-quality class libraries
A basic class gets you organized methods and state management. Production improvements include parameter validation in initialize(), consistent error handling across methods, and designing for inheritance when other classes need to extend your functionality. Well-built classes also expose only the methods other scripts need — use underscore prefixes for internal methods. The best class-based Script Includes become the foundation for entire application modules.
Before you start
- •admin role or script_writer ACL access
Sourdough: ServiceNow Monitoring and Analytics
A Chrome extension for ServiceNow Admins and Developers with essential tools, analytics, graphs and monitoring features.
Free to install. Pro $5/month after a 14-day no-card trial.
Pro requires the ServiceNow admin role. Upgrade inside the extension.
Step by step
Create the Script Include record
Navigate to System Definition > Script Includes and click New. Set Name to your class name (use PascalCase — MyUtilityClass, IntegrationHelper, etc.). Check Accessible from to 'All application scopes' if other scoped apps need this class. Leave API Name blank — it defaults to the Name value.
Name the Script Include exactly what you'll use in new ClassName() calls — ServiceNow maps the record name to the constructor.
Set up the class structure
In the Script field, start with the ServiceNow class pattern: var ClassName = Class.create(); ClassName.prototype = { }; Replace 'ClassName' with your actual class name. This creates the class constructor and prototype object that will hold your methods. Everything goes inside that prototype object.
Add the initialize method
Inside the prototype object, add initialize: function(param1, param2) { }. This is your constructor — it runs when someone calls new ClassName(). Use it to set instance properties (this.configValue = param1;), validate required parameters, and set up any initial state your methods will need.
Build your utility methods
Add methods as functions in the prototype: methodName: function(params) { // logic here }. Each method can access instance properties with this.propertyName and call other methods with this.otherMethod(). Methods should return values or throw meaningful errors — avoid silent failures.
Add the type property
At the end of the prototype object, add type: 'ClassName'. ServiceNow uses this for debugging and inheritance. It should match your class name exactly. Close the prototype object and save the Script Include.
Test instantiation and method calls
In Scripts - Background or another Script Include, test with: var util = new ClassName(constructorArgs); var result = util.methodName(params); Verify the constructor sets properties correctly, methods return expected values, and multiple instances maintain separate state.
Best practices
Validate constructor parameters in initialize() — throw clear errors for missing required config rather than letting methods fail mysteriously later.
Use underscore prefixes for internal methods (_validateData, _buildQuery) that other scripts shouldn't call directly.
Keep the constructor lightweight — don't run expensive operations or queries in initialize() since instantiation should be fast.
Return consistent data types from methods — if getRecords() sometimes returns an array and sometimes returns null, calling code breaks.
Name your Script Include record exactly what goes after 'new' — ServiceNow maps record names to constructors and mismatches cause confusing errors.
Test Your Knowledge
Quick 3-question quiz — see how your ServiceNow skills stack up.
A list view on a table with millions of records is slow. Best fix?
Select an answer to continue