Server Side Scripting
ย
โ๏ธ Server-side Scripting in ServiceNow
๐ 1. Introduction
Server-side scripting in ServiceNow refers to writing JavaScript code that executes on the server (backend) rather than in the userโs browser.
It allows developers to:
-
Manipulate records in the database
-
Enforce business logic and workflows
-
Integrate with external systems
-
Perform scheduled or background operations
๐ก Key Benefit: Server-side scripting provides data integrity, security, and automation for enterprise processes.
๐ 2. Where Server-side Scripts Run
-
Business Rules
-
Run when a record is inserted, updated, deleted, or queried.
-
Example: Automatically assign an Incident to the Service Desk group if Priority = 1.
-
-
Script Includes
-
Reusable server-side classes and functions.
-
Example: A Script Include that returns a userโs manager based on
sys_user
.
-
-
Scheduled Jobs (Scheduled Script Executions)
-
Run scripts on a schedule (daily, weekly, etc.).
-
Example: Close all Incidents older than 30 days.
-
-
Workflow Activities & Flow Designer Actions
-
Server-side scripts can run within workflows or flows for complex logic.
-
-
Background Scripts
-
Run ad-hoc scripts from System Definition โ Scripts - Background.
-
Example: Update all Problem records with a new field value.
-
-
Script Actions / Events
-
Trigger custom server logic when events are fired.
-
-
Fix Scripts
-
One-time execution scripts for hotfixes or migrations.
-
โก 3. Key Server-side APIs
ServiceNow provides powerful APIs for server scripting:
3.1 GlideRecord (Database Operations)
-
Used to query, insert, update, or delete records.
var gr = new GlideRecord('incident');
gr.addQuery('priority', 1);
gr.query();
while (gr.next()) {
gr.state = '2'; // In Progress
gr.update();
}
3.2 GlideSystem (gs)
-
Provides utility methods (logging, messages, user info).
gs.info("This is a log message.");
var user = gs.getUserDisplayName();
3.3 GlideDateTime
-
Handles date and time operations.
var gdt = new GlideDateTime();
gs.info("Current date/time: " + gdt.getDisplayValue());
3.4 GlideAjax (Server + Client Communication)
-
Allows client scripts to call server-side Script Includes.
3.5 GlideAggregate
-
Used for reporting-style queries (sum, avg, count).
var agg = new GlideAggregate('incident');
agg.addAggregate('COUNT');
agg.query();
if (agg.next()) {
gs.info("Total Incidents: " + agg.getAggregate('COUNT'));
}
๐ ๏ธ 4. Real-World Examples
Example 1: Auto-assign Incident (Business Rule)
if (current.priority == 1 && !current.assignment_group) {
current.assignment_group.setDisplayValue('Service Desk');
}
Example 2: Script Include for Reusability
var UserUtils = Class.create();
UserUtils.prototype = {
initialize: function() {},
getManager: function(userId) {
var gr = new GlideRecord('sys_user');
if (gr.get(userId)) {
return gr.manager;
}
return null;
},
type: 'UserUtils'
};
Example 3: Scheduled Job (Auto-close Incidents)
var gr = new GlideRecord('incident');
gr.addQuery('state', 'Resolved');
gr.addQuery('sys_updated_on', '<=', gs.daysAgo(30));
gr.query();
while (gr.next()) {
gr.state = 'Closed';
gr.update();
}
๐ 5. Advanced Concepts
-
Scoped Applications
-
Scripts can be scoped, meaning theyโre isolated and protected within an application.
-
-
Asynchronous Processing
-
Use gs.eventQueue() or gs.eventQueueScheduled() to trigger async logic.
-
-
Scripted REST APIs
-
Expose server-side logic as REST endpoints for integrations.
-
-
Error Handling & Logging
-
Always use
try...catch
andgs.error()
for robust debugging.
-
-
Script Optimization
-
Minimize GlideRecord loops, use indexed fields, and limit queries.
-
๐ก 6. Best Practices
-
โ Use Script Includes for reusable logic.
-
โ Always test scripts in sub-production before moving to Prod.
-
โ Use Scoped Apps for modular and upgrade-safe code.
-
โ Add comments and documentation for maintainability.
-
โ Limit Business Rule execution (avoid running unnecessary queries).
-
โ Avoid long-running scripts in Background Jobs (split into batches).
-
โ Donโt overuse synchronous server calls from client scripts.
๐ฌย Conclusion
Server-side scripting is the backbone of automation in ServiceNow.
-
It enables data manipulation, business logic, integration, and backend processing.
-
With tools like Business Rules, Script Includes, and Scheduled Jobs, admins and developers can enforce enterprise workflows effectively.
-
Following best practices ensures performance, scalability, and security.
ย
Comments
No comments yet.