difference between getValue() and setDisplayValue()

22eg105p57
Tera Contributor

Hi everyone,

I’m currently learning ServiceNow and got confused between the usage of getValue() and getDisplay() methods in scripts.

Can someone clearly explain:

What is the difference between getValue() and setDisplayValue()?

Thanks in advance!

1 ACCEPTED SOLUTION

prerna_sh
Giga Sage

Hi @22eg105p57 ,

 

getDisplayValue() is used to retrieve the displayed (user-friendly) value of a field. For example, if you're working with a reference field like "assigned_to", this method will return the user's name.

getValue(), on the other hand, retrieves the actual stored value in the database, which may differ from the display value. For reference fields, it typically returns the Sys ID or a unique value.

setDisplayValue() method sets the value of a reference field by using its display value (e.g., a user's name instead of their sys_id).

For Better understanding please go through: How to use getDisplayValue() and getValue() for ... - ServiceNow Community

If my response solves your query, please marked helpful by selecting Accept as Solution and Helpful. Let me know if anything else is required.
Thanks,
Prerna

View solution in original post

7 REPLIES 7

Deepika18
Tera Guru

Hi @22eg105p57 ,

 

getValue() will retrive the value of the field - incase of reference field you will get the sys ID.

getDisplayValue() will retrive the display value of the field - incase of reference field you will get the Display value instead of sys ID.

 

Please mark as Accepted if this resolves your issue.

 

Regards,

Deepika

Shubham_Jain
Mega Sage

@22eg105p57 Can you refer this Difference between getValue() and getDisplay() - ServiceNow Community .. Just responded 

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


Viraj Hudlikar
Tera Sage

Hello @22eg105p57 

 

getValue() = Returns the raw value stored in the database. Typically used for fields like reference, choice, date/time, etc.

ex 

var user = gr.getValue('assigned_to'); // returns sys_id

 

getDisplayValue() = Returns the human-readable display value of a field. Especially useful for reference or choice fields.

ex.

var userName = gr.getDisplayValue('assigned_to'); // returns name like "John Doe"

 

setDisplayValue() (used in GlideRecord scripting) = Sets the display value for a field, typically used when inserting or updating reference/choice fields. ServiceNow will resolve the display value to the actual value (e.g., sys_id) behind the scenes.

ex.

gr.setDisplayValue('assigned_to', 'John Doe');

 

MethodPurposeExample Output
getValue()Raw value from DBe3f2c1d3... (sys_id)
getDisplayValue()Human-readable valueJohn Doe
setDisplayValue()Set field using display valueResolves to sys_id

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @22eg105p57 

 

I believe you are doing great.

Did my reply answer your question?


If my response helped, please mark it correct and close the thread so that it benefits future readers.

As per new community feature you can mark multiple responses as correct.

 

Thanks & Regards
Viraj Hudlikar