5 Common ServiceNow Scripting Mistakes That Kill Instance Performance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hi everyone,
Over time I have seen the same performance issues appear again and again across ServiceNow instances. Most of them come down to a handful of scripting mistakes that are easy to make but expensive in production. Sharing these here in case they are useful for others.
1. GlideRecord inside a loop
This is the most common one. Every GlideRecord query inside a loop creates a separate database call.
// Bad
for (var i = 0; i < records.length; i++) {
var gr = new GlideRecord('incident');
gr.get(records[i].sys_id);
}
// Good: query once, store results in a hashmap
var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_idIN' + records.join(','));
gr.query();
while (gr.next()) {
// process here
}2. Calling update() when nothing has changed
Even if the field value is identical, calling update() still triggers Business Rules, Workflows, and generates audit records. Always check if the value has actually changed before updating.
// Bad
gr.short_description = "Test";
gr.update();
// Good
if (gr.short_description != "Test") {
gr.short_description = "Test";
gr.update();
}3. Business Rules set to run on "Always"
A Business Rule with no specific conditions runs on every single save of every record on that table. This creates unnecessary load especially on high volume tables like Incident or Task. Always add specific conditions to narrow down when the rule should fire.
4. Dot-walking reference fields without null checks
Accessing current.caller_id.email without checking if caller_id is populated first will cause errors on records where the reference field is empty, and in some cases trigger additional database lookups.
// Bad
var email = current.caller_id.email;
// Good
if (!current.caller_id.nil()) {
var email = current.caller_id.getDisplayValue('email');
}5. Using GlideRecord in Client Scripts
GlideRecord is a server side API and should never be used directly in Client Scripts. Use GlideAjax to fetch server side data instead. Using GlideRecord on the client side causes unnecessary server roundtrips and unpredictable behavior.
These are mistakes I have personally encountered and fixed. Hope this saves someone a late night troubleshooting session.
ServiceNow Developer & Admin
Builder of NowFixer (https://nowfixer.dev) | Free AI debugging tool for ServiceNow scripts
