- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 05-06-2020 01:14 AM
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
- 4,024 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks Hardit, Its working fine. I also lost 2-3hrs today before find your fix :).
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I guess this is exactly why updateMultiple() was introduced 🙂
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thx a lot !
I have exactly the same issue 🙂
But I don't understand why the loop "while" is not enough to make updates ?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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."