
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
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
- What are the difference between getDisplayValue() and getValue()
- The getDisplayValue() of GlideRecord or GlideElement
- What is problem to use dot-walking
- 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
GlideElement Use Case
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
Use Case - GlideElement class (gr.field_name.getDisplayValue())
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
Use Case 2 - return sys_id of actual reference record
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
No recommended Case - dot-walking
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.
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
- 22,479 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.