logo

NJP

gr.number and getValue(gr.number)

New article articles in ServiceNow Community · Jul 03, 2025 · article

This is once again stressing the fact how IMPORTANT concepts are.

AnishReghu_3-1751509603105.jpeg

Look at the statement inside the while loop, looks ok?

We are printing the incident number, hence 'gr.number' right?

Yes, 'grInc' here is a GlideRecord object.

BUT... Wait!

When you do the dot-walk, we easily tend to forget OR overlook the fact that we have still not accessed the value, we still are ONE STEP AWAY.

gr.getValue('number');

BUT WHY? The number field holds the record number in it.

YES, but we did not query the value of it, rather we queried it's placeholder - the 'GlideElement' object.

GlideRecord --> GlideElement -->

that tiny fact we conveniently ignored?

Hence, .getValue(), makes a perfect sense, as well!

Now, why do we tend to confuse?

var gr = new GlideRecord('incident'); gr.query(); gr.next(); gs.info(gr.number); // This logs: the incident number.

But try doing the

gs.print(typeof gr.number);

It logs the output 'object' - the GlideElement object.

The answer is auto type casting i.e. automatically invokes the .toString() method implicitly, which makes us believe that gr.number is a string value. No, it is not.

💡- Safe bet - Use .getValue() (Best Practice) - WHY?

  • It’s explicit, readable, and guaranteed to return a string
  • When comparing, pushing to arrays, or sending in REST calls, you avoid ambiguity
  • Some complex field types (like references or choices) may behave inconsistently without getValue()
When Use
Always want the raw value gr.getValue('field')
Comparing or using in logic gr.getValue()
Displaying to logs/UI gr.getDisplayValue()
You know it’s okay gr.field.toString()

Regards,

Anish

View original source

https://www.servicenow.com/community/developer-blog/gr-number-and-getvalue-gr-number/ba-p/3308471