- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
What is an Object in ServiceNow?
An object in JavaScript (and hence in ServiceNow scripting) is an instance of a class with properties (data) and methods (behavior). In ServiceNow, nearly every platform API — such as GlideRecord, GlideSystem, g_form, g_user, is exposed as an object.
ITSM Example:
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
- GlideRecord is a class.
- gr is an object instance. It holds internal state (table, filters, current record) and exposes methods to act on it.
- addQuery() and query() are methods bound to the object instance and rely on its internal state.
Method vs Function
Term |
Definition |
Example |
Function |
A standalone, stateless block of logic |
function sum(a, b) { return a + b; } |
Method |
A function attached to an object that relies on internal object state |
gr.addQuery() |
ITSM Comparison:
Function (Pure Utility):
function calculateMTTR(start, end) {
return (end.getTime() - start.getTime()) / 3600000; // returns MTTR in hours
}
Method (Context-bound):
var gr = new GlideRecord('problem');
gr.addQuery('priority', 1);
gr.query();
Core OOP Principles in ServiceNow
- Encapsulation
- Encapsulation ensures internal details of objects are hidden. Developers use public methods (like addQuery() or update()) without needing to manage SQL, cursors, or transactions.
Example:
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = 'Auto-generated';
gr.insert();
Internal state like DB connection, timestamps, and sys_id generation are encapsulated.
- Modularity (Separation of Concerns)
- Encapsulated objects promote modularity, where specific logic belongs in dedicated components (e.g., Script Includes, Flow Actions, Business Rules).
Script Include:
var IncidentHelper = Class.create();
IncidentHelper.prototype = {
getIncidentsForCaller: function(callerSysId) {
var gr = new GlideRecord('incident');
gr.addQuery('caller_id', callerSysId);
gr.query();
return gr;
},
type: 'IncidentHelper'
};
This keeps business logic out of UI Policies or Flow Actions and fosters unit testability.
- Reusability and Stateless Function Design
- Use pure functions when logic doesn’t depend on system state. Useful for calculations, string manipulations, date parsing, etc.
function formatIncidentNumber(num) {
return 'INC' + num.toString().padStart(7, '0');
}
Applying SOLID Principles in ServiceNow
S — Single Responsibility Principle
Each script should do one thing well. For example, IncidentHelper.getOpenIncidentCount() should not also email users or update SLA states.
O — Open/Closed Principle
Design components (e.g., Script Includes) that are open for extension but closed for modification. Use class inheritance or parameter injection instead of hardcoding table names.
var IncidentHelper = Class.create();
IncidentHelper.prototype = {
getOpenRecords: function(tableName) {
var gr = new GlideRecord(tableName);
gr.addQuery('state', '!=', '7');
gr.query();
return gr.getRowCount();
},
type: 'IncidentHelper'
};
L — Liskov Substitution Principle
If ProblemHelper extends IncidentHelper, it should be substitutable without breaking functionality.
I — Interface Segregation Principle
Avoid bloated Script Includes. Break large utility classes into specific interfaces like IncidentStats, IncidentNotifier, IncidentFormatter.
D — Dependency Inversion Principle
Depend on abstractions (like Glide APIs or Flow Inputs), not on concrete objects. Inject table names, roles, or thresholds via system properties or script input parameters instead of hard-coding them.
Scoped App Architecture Considerations
Namespacing and API Boundaries
- Scoped apps have isolated runtime containers.
- Use gr.isValidField() and gr.canRead() to avoid access violations.
- Avoid hardcoded table names and use current.getTableName() when authoring cross-scope utilities.
Visibility and ACLs
Objects and methods respect ACLs. A g_user.hasRole() method call might succeed in a script but still fail if ACLs block field access at runtime.
Developer Pitfalls
Function.prototype.call() and bind() in ServiceNow
While not common in most Glide scripting, call() and bind() allow passing and preserving object context explicitly, useful when delegating behavior to external utilities or callback flows.
call():
function describe() {
return 'Assigned to: ' + this.assigned_to;
}
var inc = { assigned_to: 'User1' };
gs.info(describe.call(inc));
bind():
var helper = {
name: 'IncidentHelper',
log: function() { gs.info(this.name); }
};
var logLater = helper.log.bind(helper);
logLater();
Summary
- Most ServiceNow APIs expose objects with methods, not free-floating functions.
- OOP concepts like encapsulation, modularity, and reusability are embedded in how GlideRecord, g_form, etc., work.
- SOLID principles offer a valuable mental model for enterprise-grade architecture.
- Scoped app architecture rewards developers who understand API and data boundaries.
- Debugging and maintenance become faster when method/function design is intentional.
- 165 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.