The pattern almost every developer learns first
When you start writing ServiceNow scripts, you discover that accessing gr.state directly returns a GlideElement object — not a plain string. You learn quickly to add .toString() to get something usable in comparisons and logic. The habit gets established over your first few scripts and baked in across dozens more. Nobody flags it in code review because the code works.
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
var state = gr.state.toString();
var priority = gr.priority.toString();
var assignedTo = gr.assigned_to.toString();
// logic based on these values
}
The problem is not that this is broken. It is that it hides whether you wanted the stored value or the display label, inside the loop where that choice matters most.
What toString() actually returns
When you call gr.state.toString(), the API reference says it converts the field's value to a string, and it describes getValue() as returning the value of the field in the database. The display label is what getDisplayValue() is documented to return. The two values differ by field type.
For a Choice field like state or priority, the stored value is an integer like "1" and the display value is a label like "New." The API reference notes that the database value may be a number, but the display value will be more descriptive.
For a Reference field like assigned_to or assignment_group, the database value is the sys_id and the display value is the display field of the referenced record. In the API reference's getValue() example, the raw value of opened_by is a sys_id and the display value is a user name, so the two calls return different things.
For Date/Time fields, the database value is in UTC format, while the display value is based on the user's time zone. For a simple String field the two are the same.
In a Scheduled Job that processes thousands of incidents, reading each field with getValue() when you need the stored value makes the intent of every line explicit.
getValue() — the correct default for all script logic
getValue('fieldname') returns the value of the field in the database, with no display formatting. For a reference field that is the sys_id:
The common objection: "but I need to compare against 'Resolved', not '6'." The answer is to compare against the stored value — which you look up once, or memorise. In the incident state model, the Resolved state value is 6, and ServiceNow notes the model is customizable for advanced users, so confirm the value on your own instance. Comparing gr.getValue('state') == '6' is more portable and more locale-independent than comparing against a display value that could change if someone edits the choice list label.
getDisplayValue() — when you actually need the human-readable version
There are legitimate situations where you need the display value. Use getDisplayValue('fieldname') explicitly, and only when you genuinely need it:
// Building an email notification body — human-readable values needed
var body = 'Incident ' + gr.getValue('number') + ' has been updated.
'
+ 'Priority: ' + gr.getDisplayValue('priority') // "2 - High"
+ '
State: ' + gr.getDisplayValue('state') // "In Progress"
+ '
Assigned to: ' + gr.getDisplayValue('assigned_to'); // "John Smith"
// Writing to a work note a human will read
gr.work_notes = 'Escalated to ' + gr.getDisplayValue('assignment_group');
// Populating a display-only UI field
current.setValue('u_priority_label', current.getDisplayValue('priority'));
The rule: if a human will read the value — use getDisplayValue(). If a script will process it — use getValue(). The distinction is that simple. The same logic applies in reverse when writing data — see our setValue vs setDisplayValue guide for the full breakdown.
Field reading methods — complete reference
| Method | Returns | Behavior | When to use |
|---|---|---|---|
| getValue('field') | Raw stored DB value | Returns the value of the field in the database | Script logic, comparisons, building queries |
| getDisplayValue('field') | Human-readable label | Formats the value for the user (choice label, time zone, reference display field) | Notifications, work notes, anything humans read |
| gr.field.toString() | The field's value as a string | Converts the field's value to a string | Prefer getValue() or getDisplayValue() so the intent is explicit |
| getUniqueValue() | sys_id of current record | Gets the primary key of the record | When you need the sys_id of the current record |
| gr.field (GlideElement) | GlideElement object | An object, not a string | Only when you need the full GlideElement API |
Reference fields — where this matters most
Reference fields are where the stored value and the display value differ most: the database value is a sys_id and the display value is the display field of the referenced record. Consider a Business Rule that checks assignment group on every incident update:
// Wrong: the field's value is the sys_id, not the name, so this never matches a group name
if (current.assignment_group.toString() == 'Network Operations') {
triggerNetworkAlert(current);
}
// Right: compare sys_ids directly
var networkOpsId = gs.getProperty('my.network.ops.group.sysid', '');
if (current.getValue('assignment_group') == networkOpsId) {
triggerNetworkAlert(current);
}
// ALSO GOOD — use addQuery for the filter instead
gr.addQuery('assignment_group.name', 'Network Operations');
Storing the target sys_id in a system property and comparing against it is more maintainable: if the group gets renamed, you update one property value, not every script that checked the name string. The gs.getProperty() method returns the value of a Glide property, or the alt value you pass if the property is not found.
Dot-walking with getValue
You can dot-walk reference fields as properties and call getValue() on the final element, without loading separate GlideRecord instances:
// Get the department name of the assigned_to user
var deptName = gr.assigned_to.department.name.getValue();
// Get the group manager's email
var managerEmail = gr.assignment_group.manager.email.getValue();
// Get the caller's company name
var company = gr.caller_id.company.name.getValue();
This is simpler than reading the reference field and then creating a second GlideRecord to look up related data. The API reference describes addExtraField() as a way to query dot-walked fields in a single database request, rather than performing multiple queries per dot-walked element.
Practical before/after: a nightly batch job
// BEFORE — toString() on every field, every iteration
var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^state=1');
gr.setLimit(500);
gr.query();
while (gr.next()) {
var priority = gr.priority.toString(); // the field's value as a string
var group = gr.assignment_group.toString(); // the field's value (the sys_id)
var caller = gr.caller_id.toString(); // the field's value (the sys_id)
process(priority, group, caller);
}
// AFTER — getValue() everywhere, getDisplayValue() only for output
var gr = new GlideRecord('incident');
gr.addEncodedQuery('active=true^state=1');
gr.setLimit(500);
gr.query();
while (gr.next()) {
var priority = gr.getValue('priority'); // raw "1"/"2"/"3"
var groupId = gr.getValue('assignment_group'); // sys_id
var callerId = gr.getValue('caller_id'); // sys_id
processWithIds(priority, groupId, callerId);
// only call getDisplayValue when building human-readable output
}
The setValue counterpart
The same principle applies to writing values: be explicit about what you pass. The API reference says a script would normally do a direct assignment such as gr.category = value, and that setValue() is for when the element name is a variable:
// Direct assignment is the normal approach when you know the field name
current.state = 6;
// setValue() is for when the element name is a variable
current.setValue('state', '6');
current.setValue('assigned_to', userSysId); // pass sys_id for reference fields
current.setValue('assignment_group', groupSysId);
Catching this in code review
Add .toString() on GlideElement properties to your code review mental checklist, especially inside loops. It hides whether the author meant the stored value or the display value. Two performance patterns worth checking at the same time are unconstrained GlideRecord queries (covered in the GlideRecord performance tips guide) and counting with loops instead of GlideAggregate. The Instance Scan tool catches the latter two automatically but does not flag toString() patterns — making this a developer discipline rather than an automated check.
Related scripting guides: GlideRecord performance tips · GlideAggregate guide · Encoded queries · gs object reference · Debugging scripts · Business Rule types
Why this does not show up as a problem immediately
The toString() habit is easy to miss because the code works. It returns the field's value as a string, so a test on a PDI passes. The problem is intent: a reader cannot tell whether the author wanted the stored value or the display label, and that confusion is where bugs come from, such as comparing a reference field to a group name.
This is why the Instance Scan findings around GlideRecord patterns matter — they surface accumulated technical debt that individual scripts are too small to reveal. When you run an Instance Scan and see a Business Rule flagged for performance, one of the first things to check alongside the query conditions is the field-reading pattern inside the script body.
Interview implications
The getValue() vs toString() distinction appears in most mid-level and senior ServiceNow technical interviews. The question is usually phrased as: "What is the difference between getValue() and toString() on a GlideElement?" The expected answer covers: getValue() returns the raw stored value, toString() converts the field's value to a string, and getDisplayValue() is the explicit API to use when you actually want the display value. Candidates who can explain what each method returns, not just which one to type, stand out as developers who think about execution, not just syntax.
A follow-up question that reveals deeper understanding: "What does toString() return on a Reference field?" The answer is the sys_id, because the field's value is the sys_id and getDisplayValue() is the method that returns the display field of the referenced record. That answer shows you understand the difference between stored and display values, not just the API surface.
The one-line fix that compounds over time
Of all the ServiceNow scripting habits worth establishing early in your career, the getValue() habit is among the most impactful per unit of effort. It takes no more characters to type gr.getValue('state') than gr.state.toString(). The change costs nothing during development and keeps the intent of every field read clear as your scripts grow. Make it the default. Reserve getDisplayValue() for the cases where human-readable output is genuinely needed — notifications, work notes, audit messages — and let getValue() handle everything else.
Common toString() Patterns and Their Replacements
Beyond the basic field reading scenario, toString() appears in several other contexts that each have better alternatives. When checking whether a reference field is populated, if (gr.assigned_to) evaluates the GlideElement as a boolean — truthy if populated, falsy if empty — without needing toString() at all. When building an encoded query string to pass to another GlideRecord, use gr.getEncodedQuery() on the source query rather than converting field values to strings and concatenating them manually. When comparing two GlideElement values from different records, use gr1.assigned_to.toString() == gr2.assigned_to.toString() only as a last resort. If both are reference fields pointing to the same table, comparing gr1.getValue('assigned_to') == gr2.getValue('assigned_to') is clearer and makes it explicit that you are comparing stored values. The encoded query guide covers query construction without string concatenation.