Skip to page contentSkip to chat
ServiceNow support
    • Community
      Ask questions, give advice, and connect with fellow ServiceNow professionals.
      Developer
      Build, test, and deploy applications
      Documentation
      Find detailed information about ServiceNow products, apps, features, and releases.
      Impact
      Accelerate ROI and amplify your expertise.
      Learning
      Build skills with instructor-led and online training.
      Partner
      Grow your business with promotions, news, and marketing tools
      ServiceNow
      Learn about ServiceNow products & solutions.
      Store
      Download certified apps and integrations that complement ServiceNow.
      Support
      Manage your instances, access self-help, and get technical support.
When running a script using the variable gr the result can be unexpected - Support and Troubleshooting
  • >
  • Knowledge Base
  • >
  • Support and Troubleshooting (Knowledge Base)
  • >
  • When running a script using the variable gr the result can be unexpected
KB0858301

When running a script using the variable gr the result can be unexpected


1955 Views Last updated : Apr 8, 2025 public Copy Permalink
KB Summary by Now Assist

Issue

The variable "gr" can be defined across the platform so it is recommended to not declare your GlideRecord objects using this name

There are various issues when using this so if you are using this and encounter issues attempt the details in the workaround. If you continue to experience issues and the logic of your script is valid raise a case to customer support


Below is an example of one of the highlighted issues:

-> When running a background script to update multiple records only one record is updated. An example of this could be the following script which will update multiple cmdb_ci_ip_address records.

var gr = new GlideRecord("cmdb_ci_ip_address");
//Need to set the encoded query below
var temp = "";
gr.addEncodedQuery(temp);
gr.setLimit(100);
gr.query();
gs.print(gr.getRowCount());

while (gr.next())
{
	gr.install_status = '7';
	gr.update();
}

Release

All releases

Cause

If you have declared the GlideRecord using the variable "gr" then issues can occur, such as the specified example.

The variable "gr" can be globally declared within different areas of core functionality.  This is not limited to "gr", but could occur with any variable that is declared "globally", but given the prevalence of "gr" it is more likely with this variable name.  

Then, when different threads are executing different sections of code which both use/alter the "gr" variable, its possible for one thread to impact the execution of the other by changing the object "gr" is referencing.

Resolution

There are multiple workarounds to this issue

A) Wrap the update in a function:

UpdateIPAddress();

function UpdateIPAddress()
{
	var gr = new GlideRecord("cmdb_ci_ip_address");
	//Need to set the encoded query below
	var temp = "";
	gr.addEncodedQuery(temp);
	gr.setLimit(100);
	gr.query();
	gs.print(gr.getRowCount());

	while (gr.next())
	{
		gr.install_status = '7';
		gr.update();
	}
}


B) Wrap the code in a self-invoking function:

(function() {
	var gr = new GlideRecord("cmdb_ci_ip_address");
	//Need to set the encoded query below
	var temp = "";
	gr.addEncodedQuery(temp);
	gr.setLimit(100);
	gr.query();
	gs.print(gr.getRowCount());

	while (gr.next()) {
		gr.install_status = '7';
		gr.update();
	}
})();


C) Rewrite the query to not use "gr" when declaring GlideRecord. In this example we used a variable called "ipa".  Note this makes it less likely for the issue to be encountered but if you habitually use the same variable name in different pieces of code then its could still be possible:

var ipa = new GlideRecord("cmdb_ci_ip_address");
//Need to set the encoded query below
var temp = "";
ipa.addEncodedQuery(temp);
ipa.setLimit(100);
ipa.query();

gs.print(ipa.getRowCount());

while (ipa.next()) {
	ipa.install_status = '7';
	ipa.update();
}



The world works with ServiceNow.

Sign in for more! There's more content available only to authenticated users Sign in for more!
Did this KB article help you?
Did this KB article help you?

How would you rate your Now Support digital experience?

*

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

Very unsatisfied

Unsatisfied

Neutral

Satisfied

Very satisfied

What can we improve? Please select all that apply.

What are we doing well? Please select all that apply.

Tell us more

*

Do you expect a response from this feedback?

  • Terms and conditions
  • Privacy statement
  • GDPR
  • Cookie policy
  • © 2025 ServiceNow. All rights reserved.