Displaying Labels with getDisplayValue






Mastering getDisplayValue(): Bridging the Gap Between Raw Data and Human-Readable Labels in ServiceNow



Mastering getDisplayValue(): Bridging the Gap Between Raw Data and Human-Readable Labels in ServiceNow

In the world of ServiceNow, efficiency, automation, and data integrity are paramount. We build complex workflows, manage vast amounts of data, and strive to deliver seamless user experiences. However, there’s a subtle but critical distinction that often trips up both new and experienced developers: the difference between a field’s actual value and its display value. This distinction is where our hero, the getDisplayValue() method, steps onto the stage.

Imagine you’re building a script to send an email notification about a critical incident. Would you want your users to see “Priority: 1” or “Priority: Critical”? The answer is almost always the latter. This article will be your comprehensive guide to understanding, utilizing, and mastering getDisplayValue(), ensuring your ServiceNow applications speak a language your users (and other systems) can truly understand. We’ll explore its practical applications, delve into real-world scenarios, troubleshoot common pitfalls, and even prepare you for interview questions that hinge on this fundamental concept.

The Core Conundrum: Data vs. Display

Before we celebrate getDisplayValue(), let’s understand the problem it solves. ServiceNow, like most robust database-driven platforms, stores data in an optimized, often cryptic format. This approach is excellent for database performance, referential integrity, and efficient storage, but not always for human comprehension.

Raw Data: The Machine’s Language

Consider a few common ServiceNow fields:

  • Priority: Internally, this might be stored as ‘1’, ‘2’, ‘3’, ‘4’. These are numerical codes.
  • State: For an incident, ‘6’ might represent ‘Resolved’, ‘3’ for ‘In Progress’.
  • Active: A simple boolean, ‘true’ or ‘false’.
  • Caller: This is a reference field. What’s stored is not the caller’s name, but their unique identifier, a Sys_ID (e.g., 0a00d05a0a0a0b0b007e0b519d36e2f1).
  • Created On: Stored as a UTC timestamp (e.g., 2023-10-27 15:30:00).

These raw values are perfect for database operations, filtering, and backend logic. If you need to query all incidents with ‘Priority 1’, you use `addQuery(‘priority’, ‘1’)`. If you’re linking an incident to a user, you use their Sys_ID.

Display Value: The Human’s Language

Now, think about what a user expects to see:

  • Priority: “Critical”, “High”, “Moderate”, “Low”.
  • State: “Resolved”, “In Progress”.
  • Active: “Active”, “Inactive”.
  • Caller: “John Doe” (the user’s full name).
  • Created On: “2023-10-27 08:30:00 AM PST” (formatted according to the user’s timezone and locale).

This human-readable representation is the display value. It’s what makes the platform intuitive and accessible. Without it, every user would need a decoder ring to understand their incident list or a notification email.

The challenge arises when you’re scripting. By default, when you access a field through a GlideRecord object (e.g., inc.priority), you get the *actual* value. To get the *display* value, you need a specific instruction. That instruction, my friends, is getDisplayValue().

Enter getDisplayValue(): Your Label-Displaying Hero

The getDisplayValue() method is a foundational tool in your ServiceNow scripting arsenal. Its purpose is elegantly simple: to retrieve the user-friendly, human-readable label or representation of a field’s value.

How it Works (The Mechanics)

When you call getDisplayValue() on a GlideElement object (which represents a field on a record), ServiceNow performs a lookup based on the field type:

  • Choice Lists: It finds the corresponding label for the stored numerical or string value (e.g., ‘1’ becomes ‘Critical’).
  • Reference Fields: It queries the referenced table to fetch the display field of that referenced record (e.g., a user’s Sys_ID becomes their Name).
  • Boolean Fields: It translates ‘true’/’false’ into configured labels like ‘Active’/’Inactive’.
  • Date/Time Fields: It formats the UTC timestamp according to the current user’s timezone and locale settings, making it instantly recognizable.
  • Other Fields: For simple string or integer fields, it often returns the value itself, as there’s no separate “display” for them.

Basic Usage: The Provided Example

Let’s revisit the provided reference example, which perfectly illustrates getDisplayValue() in action:

var inc = new GlideRecord('incident');
inc.addQuery ('priority', '1'); // Note: 'priority=1' in the original example is shorthand, 'priority', '1' is more explicit.
inc.query ();
while (inc.next ()){
    gs.print ('Incident Number: ' + inc.number + ', Priority: ' + inc.priority.getDisplayValue ());
}

Explanation:

  1. var inc = new GlideRecord('incident');: We initialize a GlideRecord object for the ‘incident’ table.
  2. inc.addQuery ('priority', '1');: We filter our incidents to only include those with a raw priority value of ‘1’.
  3. inc.query ();: We execute the query to fetch the matching records.
  4. while (inc.next ()){ ... }: We loop through each incident record found.
  5. gs.print ('Incident Number: ' + inc.number + ', Priority: ' + inc.priority.getDisplayValue ());:
    • inc.number: This gets the actual incident number (which coincidentally is its display value).
    • inc.priority: This would give us the raw value ‘1’.
    • inc.priority.getDisplayValue (): This is the magic! It calls the method on the priority field (which is a GlideElement object) and retrieves its human-readable label, typically “Critical”.

Result: Instead of seeing Incident Number: INC0012345, Priority: 1, you’d get Incident Number: INC0012345, Priority: Critical. A small change, but a massive improvement in clarity!

When to Unleash getDisplayValue()

You’ll find yourself reaching for this method in countless scenarios:

  • Server-Side Scripts: Business Rules, Script Includes, Fix Scripts, Workflow scripts, UI Actions.
  • Email Notifications: Crucial for ensuring the content sent to users is clear and easy to understand.
  • API Integrations: When providing data to external systems, human-readable labels are often preferred over internal codes.
  • System Logs: For debugging or auditing, printing display values makes logs much more useful.
  • Custom UI Pages/Widgets: When populating dynamic content, always aim for display values.

Diving Deeper: Practical Examples and Real-World Scenarios

Let’s move beyond the basics and explore more complex and common scenarios where getDisplayValue() shines.

Scenario 1: Reference Fields – The Most Common Use Case

Reference fields are where getDisplayValue() often performs its most dramatic transformation. Consider the `caller_id` field on an incident.

var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('active', true);
incidentGR.setLimit(5); // Just for example, limiting to 5 records
incidentGR.query();

while (incidentGR.next()) {
    var callerSysId = incidentGR.caller_id; // Actual value: sys_id of the user
    var callerName = incidentGR.caller_id.getDisplayValue(); // Display value: Name of the user

    gs.print('Incident: ' + incidentGR.number + ', Caller Sys_ID: ' + callerSysId + ', Caller Name: ' + callerName);
}

Output will be something like:

Incident: INC0010001, Caller Sys_ID: abcd1234efgh5678ijkl9012mnop3456, Caller Name: Abel Tuter
Incident: INC0010002, Caller Sys_ID: 1234abcd5678efgh9012ijkl3456mnop, Caller Name: Beth Anglin

Without getDisplayValue(), you’d be stuck with cryptic Sys_IDs in your logs or notifications, forcing users to look up who abcd1234efgh5678ijkl9012mnop3456 actually is.

Scenario 2: Choice Fields Beyond Priority (State, Category, etc.)

Any field that uses a choice list benefits immensely. Think about an Incident’s `state` field.

var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('state', '6'); // Querying for incidents in 'Resolved' state (raw value '6')
incidentGR.setLimit(3);
incidentGR.query();

while (incidentGR.next()) {
    gs.print('Incident: ' + incidentGR.number + ', Raw State: ' + incidentGR.state + ', Display State: ' + incidentGR.state.getDisplayValue());
}

Expected Output:

Incident: INC0010005, Raw State: 6, Display State: Resolved
Incident: INC0010006, Raw State: 6, Display State: Resolved

This is invaluable for reporting and communication. Showing “Resolved” is infinitely more meaningful than “6”.

Scenario 3: Date/Time Fields and User Localization

getDisplayValue() on date/time fields is a subtle but powerful feature. It automatically formats the date and time according to the user’s current session timezone and locale settings.

var incidentGR = new GlideRecord('incident');
incidentGR.get('a2b2c2d2e2f2g2h2i2j2k2l2m2n2o2p2'); // Replace with a valid incident Sys_ID

if (incidentGR.isValidRecord()) {
    var rawCreatedOn = incidentGR.sys_created_on; // Raw UTC value
    var displayCreatedOn = incidentGR.sys_created_on.getDisplayValue(); // Formatted based on user's timezone/locale

    gs.print('Incident Number: ' + incidentGR.number);
    gs.print('Raw Created On (UTC): ' + rawCreatedOn);
    gs.print('Display Created On (User Local): ' + displayCreatedOn);
}

If the raw value is 2023-10-27 15:30:00 (UTC) and your user is in PST (UTC-7), the display value might be 2023-10-27 08:30:00 AM PST. This prevents confusion and improves the global usability of your applications.

Scenario 4: Building Dynamic Email Notifications

This is perhaps one of the most common and impactful uses. When crafting an email script for an incident update, you want all fields to be user-friendly.

/* Email Script Example (within a Notification) */
(function runMailScript(current, template, email, scriptless) {

    email.setSubject('Incident ' + current.number + ' has been updated!');

    template.print('<p>Hello ' + current.opened_by.getDisplayValue() + ',</p>');
    template.print('<p>Incident ' + current.number + ' has been updated with the following details:</p>');
    template.print('<p><strong>Priority:</strong> ' + current.priority.getDisplayValue() + '<br/>');
    template.print('<strong>State:</strong> ' + current.state.getDisplayValue() + '<br/>');
    template.print('<strong>Assigned To:</strong> ' + current.assigned_to.getDisplayValue() + '<br/>');
    template.print('<strong>Updated By:</strong> ' + current.sys_updated_by.getDisplayValue() + ' on ' + current.sys_updated_on.getDisplayValue() + '</p>');

})(current, template, email, scriptless);

Here, every reference and choice field is presented with its clear, readable label, making the email professional and easy to digest for the recipient.

Nuances and Best Practices

While getDisplayValue() is a powerful tool, understanding its nuances and adhering to best practices ensures optimal performance and correct behavior.

Alternative Syntax: record.getDisplayValue('field_name')

You might encounter or prefer an alternative way to get a field’s display value:

var inc = new GlideRecord('incident');
inc.get('a2b2c2d2e2f2g2h2i2j2k2l2m2n2o2p2'); // Load a specific incident

var priorityDisplay = inc.getDisplayValue('priority'); // Using the alternative syntax
var callerDisplay = inc.getDisplayValue('caller_id');

gs.print('Priority: ' + priorityDisplay + ', Caller: ' + callerDisplay);

Key Differences and When to Use Which:

  • inc.priority.getDisplayValue(): You call the method directly on the GlideElement object (inc.priority). This is often more intuitive when you know the field name upfront.
  • inc.getDisplayValue('priority'): You call the method on the GlideRecord object (inc) and pass the field name as a string argument. This is particularly useful when the field name itself is dynamic (e.g., stored in a variable) or for consistency when getting multiple display values.

Both achieve the same result. Choose the one that best fits your coding style or specific scenario. For simple, known field access, record.field_name.getDisplayValue() is often quicker to type and read.

Performance Considerations (and Why You Usually Don’t Need to Worry)

Every method call has a performance cost. For reference fields, getDisplayValue() might trigger an additional database lookup to fetch the display name from the referenced table. For choice lists, it’s a quick lookup within definitions.

Should you be concerned?
Generally, no. For typical scripting scenarios (business rules, email scripts, even moderate data processing), the overhead of getDisplayValue() is negligible. ServiceNow is optimized for these operations.

When to be mindful:
If you’re processing tens of thousands of records in a single, synchronous script, and calling getDisplayValue() for multiple reference fields on each record, you *might* observe a performance impact. In such extreme cases, consider alternatives like batch lookups (if building a custom integration) or processing in chunks. However, these are advanced optimization scenarios that most developers won’t encounter daily.

When NOT to Use getDisplayValue()

Knowing when *not* to use a tool is just as important as knowing when to use it.

  • Database Operations (Queries & Updates): Never use display values when performing `addQuery()` or `setValue()`. Always use the actual, stored value.
    // Correct: Querying by actual value
    inc.addQuery('priority', '1');
    
    // Incorrect: This will fail as 'Critical' is not the stored value
    // inc.addQuery('priority', 'Critical');
    // Correct: Setting actual value
    inc.setValue('state', '6');
    
    // Incorrect: This will fail as 'Resolved' is not the stored value
    // inc.setValue('state', 'Resolved');
  • Unique Identification: For unique identifiers (like `sys_id`), the actual value is always superior. Display values can be duplicated (e.g., two users named “John Doe”), but Sys_IDs are globally unique.
  • Raw Data Exports/Integrations: Sometimes, external systems or analytical tools prefer raw, normalized data (e.g., ‘1’ for priority) to simplify their own processing or data warehousing. Always confirm the requirements of the consuming system.

Troubleshooting Common getDisplayValue() Issues

Even heroes have their off days. Here are some common problems you might encounter with getDisplayValue() and how to troubleshoot them.

Issue 1: Getting `undefined` or `null`

You expect a display value, but you get nothing.

  • Cause A: The Field Doesn’t Exist (Typo). You’ve misspelled the field name or it simply doesn’t exist on the table.
    // Assuming 'priority' exists, but 'priorrity' does not
    gs.print(inc.priorrity.getDisplayValue()); // Will likely return undefined.

    Solution: Double-check the field name against the table dictionary. Ensure it’s correct.

  • Cause B: Record Not Found. Your `GlideRecord` query didn’t return any records, or you forgot `inc.next()` inside a loop.
    // This won't work if 'get' fails or 'query' returns no records
    var inc = new GlideRecord('incident');
    inc.get('nonexistent_sys_id'); // If get fails, inc.priority will be null/undefined
    gs.print(inc.priority.getDisplayValue());

    Solution: Always verify that your `GlideRecord` has a valid record using `inc.isValidRecord()` after `get()` or ensure your `while (inc.next())` loop is correctly structured.

  • Cause C: Field is Empty. The field simply has no value stored (e.g., `assigned_to` is empty). Calling `getDisplayValue()` on an empty GlideElement will return an empty string or null.
    var inc = new GlideRecord('incident');
    inc.get('incident_sys_id_with_no_assignee');
    gs.print(inc.assigned_to.getDisplayValue()); // Will print an empty string or null

    Solution: This isn’t an error, but expected behavior. Add checks for empty values if you need to provide a fallback:

    var assignedTo = inc.assigned_to.getDisplayValue();
    gs.print('Assigned To: ' + (assignedTo ? assignedTo : 'Unassigned'));

Issue 2: Getting the *Actual* Value Instead of Display Value

You used `getDisplayValue()`, but it still printed ‘1’ instead of ‘Critical’.

  • Cause A: Forgetting `getDisplayValue()` Altogether. The simplest explanation.
    // Missing getDisplayValue()
    gs.print(inc.priority); // Prints '1'

    Solution: Ensure you are explicitly calling `.getDisplayValue()` on the GlideElement.

  • Cause B: Calling on the Wrong Object. You might have called `getDisplayValue()` on the `GlideRecord` object itself, which returns the display value of the *record* (usually the number field), not a specific field.
    // This gets the display value of the incident record (e.g., INC0012345)
    gs.print(inc.getDisplayValue());
    
    // This is what you want for the priority field
    gs.print(inc.priority.getDisplayValue());

    Solution: Remember `record.field_name.getDisplayValue()` or `record.getDisplayValue(‘field_name’)`.

Issue 3: Incorrect Display Value (e.g., Wrong Language/Format for Dates)

The display value is coming back in the wrong language or an unexpected date format.

  • Cause A: User’s Session Settings. `getDisplayValue()` is context-aware. If you’re running a background script or business rule, it often runs in the system’s context or the context of the user who triggered it. Date/time formats and language preferences are pulled from that user’s profile or the system defaults.
    Solution: Verify the language and timezone settings for the user under whose context the script is running. If you need a specific, non-user-dependent format, you might need to use `GlideDateTime` methods for explicit formatting.

Interview Relevance: Mastering getDisplayValue() for Success

If you’re aiming for a ServiceNow developer or administrator role, expect to be asked about `getDisplayValue()`. It’s a common interview question because it quickly reveals a candidate’s foundational understanding of ServiceNow’s data model and scripting best practices.

Why Interviewers Ask About It:

  1. Understanding of Data Model: It gauges whether you grasp the crucial distinction between raw database values and user-friendly labels. This is fundamental to working effectively in ServiceNow.
  2. Scripting Proficiency: It tests your knowledge of core GlideRecord/GlideElement methods and how to apply them correctly.
  3. User Experience Awareness: It shows you understand the importance of presenting information in a clear, consumable way for end-users, which is vital for building good applications.
  4. Problem-Solving: Explaining when to use it (and when not to) demonstrates critical thinking.

Key Points to Highlight in an Interview:

  • getDisplayValue() is essential for converting a field’s internal, database-stored value into its human-readable, user-facing label.”
  • “It bridges the gap between the machine’s language (Sys_IDs, numerical codes) and the user’s language (names, descriptive labels).”
  • “I primarily use it for anything customer-facing: email notifications, custom UI pages, external integrations, and detailed logging.”
  • “For reference fields like `caller_id` or `assigned_to`, it fetches the user’s name instead of their Sys_ID.”
  • “For choice fields like `priority` or `state`, it gives the text label (‘Critical’, ‘Resolved’) instead of the number (‘1’, ‘6’).”
  • “It’s also great for date/time fields, as it formats them according to the user’s timezone.”
  • “Crucially, I know not to use it for database queries or updates; those always require the actual stored value.”
  • “I’m familiar with both `record.field_name.getDisplayValue()` and `record.getDisplayValue(‘field_name’)`.”

Sample Interview Questions:

Be prepared to answer questions like these:

  1. “Can you explain the difference between current.state and current.state.getDisplayValue() in a Business Rule?”
    • Expected Answer: `current.state` gives the raw numerical value (e.g., ‘6’ for Resolved), used for database operations like queries. `current.state.getDisplayValue()` provides the human-readable label (e.g., ‘Resolved’), used for display purposes like notifications or UI.
  2. “You need to send an email notification about an incident. How do you ensure the ‘Assigned To’ field shows the user’s name rather than a Sys_ID?”
    • Expected Answer: I would use `current.assigned_to.getDisplayValue()` within the email script.
  3. “When would you *not* use getDisplayValue()?”
    • Expected Answer: I would not use it when performing database queries (e.g., `addQuery`) or setting field values (e.g., `setValue`), as these operations require the actual stored value. Also, if an external integration specifically requests raw data.
  4. “You’re seeing a date/time field displayed in UTC, but your user expects their local time. How might getDisplayValue() help, and what could be a reason it’s still showing UTC?”
    • Expected Answer: `getDisplayValue()` on a date/time field should automatically format it to the user’s local time and timezone. If it’s still showing UTC, it might be that the script is running in a system context without a specific user’s timezone, or the user’s profile settings are incorrect.

Conclusion

getDisplayValue() might seem like a small method, but its impact on the usability and clarity of your ServiceNow applications is immense. It’s the bridge that connects the efficient, raw data of the database to the intuitive, human-friendly interface your users expect.

By mastering this method, you not only write more effective scripts but also demonstrate a deeper understanding of the ServiceNow platform – a skill highly valued in any development or administrative role. From crafting crystal-clear email notifications to building robust integrations, knowing when and how to wield getDisplayValue() is a hallmark of a proficient ServiceNow professional.

So, go forth, explore, and let getDisplayValue() transform your raw data into a symphony of clear, concise, and meaningful labels. Your users (and your interviewers) will thank you for it!


Scroll to Top