Community Alums
Not applicable

Hi Guys,

I was facing an issue where my following simple script was updating only one record at a time. 

var gr = new GlideRecord('cmdb_ci_server');
gr.addEncodedQuery('install_status=16');
gr.query();
while(gr.next())

{
gr.operational_status= 110;
gr.update();

}

 

It got my head scratching for some time and after that I tried inserting my code in a self-invoking function and it worked brilliantly. Just to save time of you guys who might face this issue in future the code is here.

(function (){ 
var gr = new GlideRecord('cmdb_ci_server');
gr.addEncodedQuery('install_status=16');
gr.query();
while(gr.next()){
	gr.operational_status= 110;
	gr.update();
} }());

 

Please mark this article Helpful, if it helped you.

Cheers,

Hardit Singh

 

Comments
balaji_prusty1
Giga Guru

Thanks Hardit, Its working fine. I also lost 2-3hrs today before find your fix :).

CezaryBasta
Tera Guru

I guess this is exactly why updateMultiple() was introduced 🙂

elodieInIt
Tera Explorer

Thx a lot !

I have exactly the same issue 🙂

 

But I don't understand why the loop "while" is not enough to make updates ?

Mwatkins
ServiceNow Employee
ServiceNow Employee

It is likely that this comes down to Javascript variables and scope. Your "gr" variable is probably getting overwritten by a variable with the same name in another script, that is getting triggered by the update operation. I suspect you could also solve the same problem by renaming your variable from "gr" to something less generic.

 

There's a discussion about it in the Scripting Best Practices page under the topic "Self-Executing Functions":
https://developer.servicenow.com/dev.do#!/guides/tokyo/now-platform/tpb-guide/scripting_technical_be...

 

"...By enclosing your script in a self-executing function you can ensure that the script does not impact other areas of the product, such as by overwriting global variables."

Version history
Last update:
‎05-06-2020 01:14 AM
Updated by:
Community Alums