Get Field Values from GlideRecord in ServiceNow
- nathanlee142
- Mar 19
- 2 min read
Updated: Mar 30

Getting accurate field values from GlideRecord objects is crucial when scripting in ServiceNow. While there are multiple ways to achieve this, choosing the best practice ensures your scripts run efficiently and avoid common pitfalls. This article covers the safest method to retrieve field values from GlideRecord and explains why it's preferred by seasoned ServiceNow developers.
Understanding the Common Approaches
When retrieving field values from a GlideRecord object, developers often use either dot-walking or the built-in getValue() method:
Dot-Walking:
var gr = new GlideRecord('incident');
gr.addQuery('active', 'true');
gr.query();
if (gr.next()) {
var sysId = gr.sys_id; // Dot-walking method
}
getValue() Method:
var gr = new GlideRecord('incident');
gr.addQuery('active', 'true');
gr.query();
if (gr.next()) {
var sysId = gr.getValue('sys_id'); // getValue() method
}
At first glance, these methods might seem interchangeable, but they differ significantly, particularly when handling references and loops.
Why the getValue() Method is Preferred
The ServiceNow best practice strongly recommends using the getValue() method over dot-walking.
Here's why:
Prevents Unintended Pointer References Dot-walking accesses a pointer directly. This means if you use it within loops, it could inadvertently cause all values to reflect only the last record processed. Conversely, the getValue() method returns a copy of the field value, ensuring each record retains its correct and intended data.
For example:
var incidentIds = [];
while (gr.next()) {
incidentIds.push(gr.getValue('sys_id'));
// Correctly stores each unique sys_id
}
Better Stability in Scripts Using getValue() enhances script stability by explicitly fetching field values as strings, preventing issues where dot-walking might return an object reference instead of an expected value. This is especially critical in loops, where direct field references could lead to unintended data overwrites.
Dynamic and Flexible The getValue() method accepts variables as field names. This flexibility is invaluable in dynamic scripts where the field names aren't hard-coded.
Here's a example:
var fieldName = 'caller_id';
var gr = new GlideRecord('incident');
gr.query();
if (gr.next()) {
var caller = gr.getValue(fieldName);
}
Advanced Usage: Using getUniqueValue()
Additionally, ServiceNow provides another specialized method for retrieving the sys_id directly: getUniqueValue(). This method is highly recommended when specifically accessing a record's unique identifier because it's concise and highly readable:
var sysId = gr.getUniqueValue();
Using getUniqueValue() instead of getValue('sys_id') clearly communicates the intent, enhancing the readability of your scripts.
Practical Considerations
Always prefer getValue() when retrieving values in scripts, especially within loops.
Use getUniqueValue() specifically for fetching sys_id values.
Avoid dot-walking directly to sys_id or other reference fields, as it might cause unexpected behaviors in your scripts.
Conclusion
To write robust and maintainable scripts in ServiceNow, it is essential to follow best practices when working with GlideRecord objects. Using the getValue() method or getUniqueValue() for retrieving values offers safety, clarity, and reliability, significantly improving script quality and maintainability. Keep these practices in mind when developing your next ServiceNow application or workflow automation.