NowSpectrum Issue #5 · Weekly Tips

Hey {{first_name}}, 👋

This week — a one-line mistake that every ServiceNow developer makes at least once, and why it silently corrupts your data without throwing a single error.

The mistake —

Using setValue() with a display value instead of the actual stored value. It works. It saves. And it puts the wrong data in your database every single time.

The Problem

Take a reference field like state on an incident. The display value is "In Progress". The stored value is 2. Most developers write this:

✕ Stores the wrong value silently
var gr = new GlideRecord('incident');
gr.get(sys_id);
gr.setValue('state', 'In Progress');  // ✕ stores "In Progress", not 2
gr.update();

No error thrown. Record saves. But state is now a string instead of an integer — breaking every workflow condition, every report filter, and every SLA that checks for state = 2.

The Fix

Always pass the stored value to setValue(). For choice fields, that's the integer or key. For reference fields, that's the sys_id. Use setDisplayValue() only when you intentionally want to resolve a display label to its stored value.

✓ Correct approaches
var gr = new GlideRecord('incident');
gr.get(sys_id);

// Option 1 — pass the stored integer directly
gr.setValue('state', 2);  // ✓ 2 = In Progress

// Option 2 — resolve display label to stored value
gr.setDisplayValue('state', 'In Progress');  // ✓ resolves safely

gr.update();

Rule of thumb: setValue() = stored value (integer, sys_id, true/false). setDisplayValue() = label (string you'd see on screen). Never mix them.

Where This Breaks Most Often

→ Choice fields — state, priority, urgency, impact
→ Reference fields set via Business Rules running on import sets
→ Scripted REST APIs receiving display values from external systems

Search your Business Rules and Script Includes for setValue( where the second argument is a quoted string on a choice or reference field. Those are your candidates.

The full setValue vs setDisplayValue breakdown — with field types, edge cases, and import set patterns — is on nowspectrum.com Read full article →

— The NowSpectrum Team

NowSpectrum nowspectrum.com