Notifications

42 views

Description

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 or Environment

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();
}


Article Information

Last Updated:2020-09-15 01:27:57
Published:2020-09-15