Stop Using toString() in Your GlideRecord Loops

One small change that makes your scripts noticeably faster. Most ServiceNow developers do this without thinking — here is why getValue() is always the right call, and what is actually happening under the hood.

The pattern most developers use

If you have been writing ServiceNow scripts for any length of time, you have probably written something like this:

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
  var state = gr.state.toString();
  var priority = gr.priority.toString();
  // do something with state and priority
}

It works. The code runs. So what is the problem?

What toString() actually does

When you call gr.state.toString(), ServiceNow does not just return the stored value. It triggers the field's display value resolution process — it goes through the field type handler to produce a human-readable output.

For a Choice field like state, this means resolving the choice label from the choice list. For a Reference field, it means looking up the display value of the referenced record. For a simple String field it is mostly harmless, but the overhead still exists.

In a loop over 10 records, the difference is negligible. In a loop over 500 records, it adds up. In a scheduled job processing thousands of records, it can add meaningful seconds to your execution time.

The right way: getValue()

var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
  var state = gr.getValue('state');
  var priority = gr.getValue('priority');
  // do something with state and priority
}

getValue('field') returns the raw database value directly — no display value resolution, no type handler overhead. For a Choice field like state, you get back the stored key (e.g., 1 for New, 2 for In Progress). For a Reference field you get the sys_id of the referenced record.

When to use each method

This is the part most guides skip. It is not that toString() is always wrong — it is that most developers use it when they do not need it.

  • Use getValue() when you are comparing values, building encoded queries, or passing values to other scripts. You want the raw stored value.
  • Use getDisplayValue() when you are building a notification message, populating a UI display, or writing to a work note that a human will read. You want the human-readable label.
  • Avoid toString() in scripts. It is less explicit than the two options above and adds unnecessary processing.

A real example where this matters

Imagine a scheduled job that runs nightly and processes 2,000 active incidents — checking priorities and updating assignment groups. Using toString() on three fields per record = 6,000 unnecessary display value lookups per run. Switch to getValue() and the job runs noticeably faster with lower server load.

// AVOID this in loops
var assigned = gr.assigned_to.toString();  // resolves display name
var group = gr.assignment_group.toString(); // resolves group name

// DO this instead
var assignedId = gr.getValue('assigned_to');       // raw sys_id
var groupId = gr.getValue('assignment_group');     // raw sys_id

// Only use getDisplayValue() if you actually need the name
var groupName = gr.getDisplayValue('assignment_group'); // only when needed

The rule to remember

If you are using the value for logic, comparison, or script processing — use getValue(). If you are using the value for display or human-readable output — use getDisplayValue(). Never default to toString() just because it is familiar.

It is a small habit change. Over a career of writing ServiceNow scripts, it makes a real difference.

Want the complete reference?

This article is part of the NowSpectrum knowledge library. Browse all products for cheat sheets, interview prep, and deep-dive reference guides.

Browse All Products →
← Back to all posts