Change toString() to getValue() in a script include

rafas_10
Tera Guru

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!

3 REPLIES 3

Bhimashankar H
Mega Sage

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

Use Case Use getValue() Use toString()
Get raw database value (e.g., Sys ID)YesNo
Get display value (e.g., user's full name)NoYes
Compare/reference internal field valueYesNo
Output for user-facing messageNoYes
Numeric/date/choice field as stringYes (for storage/comparison)Yes (for formatted display)
Concatenating mixed data types as stringSometimes (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!

Ankur Bawiskar
Tera Patron
Tera Patron

@rafas_10 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Bhimashankar H
Mega Sage

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