John Zhang1
Kilo Patron
Kilo Patron

When we are retrieves the records from GlideRecord or GlideElement, in most cases, don’t use dot-walking to get values from a record because dot-walking retrieves the entire object instead of the field value. Retrieving the object uses more storage and might cause undesirable performance issue when used in arrays or in Service Portal.

 

Objectives

  1. What are the difference between getDisplayValue() and getValue()
  2. The getDisplayValue() of GlideRecord or GlideElement
  3. What is problem to use dot-walking
  4. Summary

What are the difference between getDisplayValue() and getValue()

 The  getDisplayValue() method is used when you need to retrieve the display value of a field. This is particularly useful when you want to display the value of a field as it appears to the user on the form, including any formatting or translations. If you have a choice field with display values , calling getDisplayValue() on that field would return one of those values depending on what is selected.

 

Use  getValue( ) method when you need to retrieve the actual value of a field, which might be different from its display value. This method returns the raw value stored in the database.  If you have a choice field with display values "High", "Medium", and "Low", calling  getValue( ) on that field would return the underlying value.

 

From performance perfect,   getValue( ) is generally faster and lighter in terms of performance compared to getDisplayValue()  because it directly retrieves the stored value without additional processing, but getDisplayValue()  but getDisplayValue()  may involve additional processing to retrieve value.

 

GlideRecord method Use Cases

 

JohnZhang1_2-1714225737667.png

JohnZhang1_3-1714225794225.png

 

JohnZhang1_5-1714225966959.png

JohnZhang1_4-1714225941460.png

 

GlideElement Use Case

JohnZhang1_5-1713628622321.png

JohnZhang1_4-1713628597957.png

 

JohnZhang1_1-1713630073947.png

JohnZhang1_0-1713630054218.png

 

The getDisplayValue() ) gives you the value that is displayed for the field's value. You might think a field's value is what it is, but certain field types can have a disparity between the actual value, and the display value. For example, drop-down fields can have a label as well as a value.

 

The getDisplayValue() method for GlideRecord or GlideElement

Both the GlideRecord and GlideElement classes can use a getDisplayValue() method. While the method of the GlideRecord object (gr.getDisplayValue()) gets the display value for the record based on the field that's marked as the Display Value in the field dictionary, the getDisplayValue() method of the GlideElement class (gr.field_name.getDisplayValue()) gives you the value that is displayed for the field's value.

 

Use Case -GlideRecord's Display Value

JohnZhang1_1-1713627571666.png

JohnZhang1_0-1713627517990.png

 

Use Case - GlideElement class (gr.field_name.getDisplayValue())

JohnZhang1_3-1713627859642.png

JohnZhang1_2-1713627835944.png

 

if  the field type is dropdown or reference, the drop-down fields can have a label as well as a value,  It's the value that gets stored in the actual database.  However the reference type  have both display values:  1) display value from the referenced table); 2) the actual values from the referenced record sysID

 

Use Cases 1 -get the display value from the referenced table

JohnZhang1_0-1713023082106.png

JohnZhang1_1-1713023186127.png

Use Case 2 - return sys_id of actual reference record

JohnZhang1_2-1713023273937.png

JohnZhang1_3-1713023333035.png

 

What is problem to use dot-walking

The big problem is performance because dot-walking involves traversing multiple tables and relationships to access a field value. This can be computationally expensive, especially if you're accessing fields across multiple tables or if there are a large number of records involved. It also relies on relationships between tables, and if those relationships change or if there are any inconsistencies in the data model, it can lead to errors or unexpected results.

 

However both getDisplayValue()  or  getValue( ) directly be used on GlideRecord or GlideElement objects typically incurs less overhead and is faster as well as  directly  accessing the data from the specific record you're working with, without relying on potentially complex relationships.

 

Recommended Case

JohnZhang1_10-1714227491007.png

JohnZhang1_11-1714227526405.png

 

No recommended Case - dot-walking

JohnZhang1_8-1714227241230.png

JohnZhang1_9-1714227267777.png

 

If dot-walking through a GlideElement object is necessary, use the toString() method to retrieve values. For example, you might need the current caller's manager sys_id to set another reference field.  

 

JohnZhang1_0-1714250180792.png

 Summary

Choosing between getDisplayValue()  and  getValue( ) depends on your specific use case. If you need to manipulate or compare the actual value of a field, use  getValue( ). If you need to display the field to the user exactly as it appears on the form, use getDisplayValue().

 

While dot-walking can be convenient in some cases, such as when you need to access fields from related records without explicitly querying those records, it's generally recommended to use getDisplayValue()  or  getValue( ) when working directly with GlideRecord or GlideElement objects for improved performance, data consistency, clarity, and control.

 

If you enjoy my ServiceNow posts, please be sure to follow me on LinkedIn and my ServiceNow YouTubes Channels

 

 

 

 

 

 

 

 

 

3 Comments