Change toString() to getValue() in a script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2025 07:39 AM
Hi everyone,
During the scan findings of my update set, It brought the suggestion of changing toSring() to getValue().
The issue here is that it is not retrieving the same values with that suggestion, meaning when I use toString in my script it has the right amount of visibility but when I use getValue the visibility of some things is decreased.
When should I use getValue and where should I keep toString?
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2025 08:22 AM
Hi @rafas_10 ,
getValue():
Purpose:
getValue(fieldName) is a GlideRecord method used to retrieve the raw value of a field as a string, exactly as it is stored in the database.Common Use Cases:
When you need the underlying database value, especially for reference, choice, date/time, or numeric fields.
When you want to compare or log the database value without formatting.
Useful in scripts where field values might be objects (like GlideElement for reference fields) and you need the string Sys ID.
Example:
var gr = new GlideRecord('incident');
gr.get('sys_id_here');
var callerId = gr.getValue('caller_id'); // Gets the Sys ID as a string
Imp Note:
Use getValue() when accessing a field not directly as a JavaScript string, or when iterating through fields where the value type may not be reliably "just a string" (e.g., reference/object fields).
toString():
Purpose:
toString() in ServiceNow scripting converts an object or value to its string representation. On GlideElement fields, it calls the element’s internal toString() method.Common Use Cases:
When you want the display value of a field (what the user sees on the form—like the actual name instead of Sys ID).
When working with JavaScript objects that need to be rendered or concatenated as strings.
To ensure you pass a string instead of an object in string operations.
- Example
var gr = new GlideRecord('incident');
gr.get('sys_id_here');
var callerName = gr.caller_id.toString(); // Returns the display value (e.g., caller's name)
Imp Note:
Use toString() when you need the field’s human-readable format, such as for email templates, logs, or UI displays.
Comparison Table
Get raw database value (e.g., Sys ID) | Yes | No |
Get display value (e.g., user's full name) | No | Yes |
Compare/reference internal field value | Yes | No |
Output for user-facing message | No | Yes |
Numeric/date/choice field as string | Yes (for storage/comparison) | Yes (for formatted display) |
Concatenating mixed data types as string | Sometimes (if unsure, use .toString()) | Yes |
Practical Example
Suppose you have a reference field assigned_to on incident:
gr.getValue('assigned_to') returns the Sys ID of the user.
gr.assigned_to.toString() returns the display value (e.g., "John Doe").
In Short
Use getValue() to retrieve the raw, stored value from a GlideRecord—best for logic, condition checks, and technical references.
Use toString() when you need a readable or formatted value for display, concatenation, or communication with end users.
Thanks,
Bhimashankar H
-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2025 08:26 AM
both means the same and should work fine.
getValue()
-> Safest method and use when you want the exact string value stored in the database.
-> use when you are iterating over GlideRecord and want to use value for comparison, etc
-> field is string, choice, reference or other types
toString()
-> can be used on GlideDateTime etc values
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2025 10:09 PM
Hi @rafas_10 ,
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Future readers with similar kind of question will easily find out. Thanks in advance!
Regards,
Bhimashankar H