Although most of our free time has been spent focusing on writing the Learning ServiceNow book, we wanted to post a quick Pro-Tip to address an issue we've seen impact performance on nearly every instance we've worked on: The use of loops to update a single field on a large number of records in the database.
Let's say you've got a client who wants to run a scheduled job that'll auto-close Incidents in "In Progress" status, that haven't been updated for 30 days, by setting their state to a custom value, "stale" (or, since State is an integer field, 10). Let's see what a script for that purpose might look like, as it's usually written:
closeStaleIncidents();
function closeStaleIncidents() {
var staleStateValue = 10;
var strEncodedQuery = 'sys_updated_onRELATIVELT@dayofweek@ago@30^state=2';
var grInc = new GlideRecord('incident');
grInc.addQuery(strEncodedQuery);
grInc.query();
while (grInc.next()) {
grInc.setValue('state', staleStateValue);
grInc.update();
}
}
As you can see, we're getting a GlideRecord on the incident table, adding a query, then looping through each record returned from that query, setting a value on it, and updating it in the database.
But wait a minute, we're performing exactly the same change on each record returned from this query. What if instead of grabbing each record one at a time, we could just tell the database that we want to update all of these records in the same way? We can do so, using the updateMultiple() method! Let's have a look:
closeStaleIncidents();
function closeStaleIncidents() {
var staleState = 10;
var query = 'sys_updated_onRELATIVELT@dayofweek@ago@30^state=2';
var gr = new GlideRecord('incident');
gr.addQuery(query);
gr.setValue('state', staleState);
gr.updateMultiple();
}
As you can see, we don't actually retrieve a record using .next() at all; instead, we take the "scaffolding" of the GlideRecord (the empty GlideRecord, populated with the empty GlideElements for each field already set up, by using the .query() method). Using that empty scaffold, we then update one of the GlideElement values using .setValue(), so that state is the only populated field in this GlideRecord. Then, when we call .updateMultiple(), we're telling the database to propagate this change across all records which match the query on our GlideRecord object (gr)!
This is much more efficient than looping through every record that would be returned from our query, retrieving it from the database, setting each value one at a time, and saving each record back to the database; however, we do have to take a special precaution here. You'll notice that I used .setValue() to set the value of the field, rather than directly setting the GlideElement for the state field as though it were a String value - for example:
grInc.state = '10';
This is important. Personally, I recommend always using the .setValue()
method rather than directly setting the field as a property of the GlideRecord object (except in the case of journal fields) to avoid issues like this. In the case of .updateMultiple()
however, it is especially important. If we fail to use the .setValue()
method to set the new field value before using .updateMultiple()
, that can result in all records in the table being updated, rather than just those returned from our query! (For example, if we just do grInc.state = '10';
as you see above.)
You can read more about how important it is to use .setValue()
(and .getValue()
) in this related SN Pro Tips article.
SN Pro Tips is owned and written by The SN Guys, ServiceNow consultants. If your organization could benefit from our services, we humbly welcome you to schedule some time to chat and share your goals with us, so we can help your business Grow With Purpose.
- March 2024
-
February 2024
- Feb 12, 2024 5 Lessons About Programming From Richard Feynman
- July 2023
- May 2023
- April 2023
-
December 2022
- Dec 13, 2022 ServiceNow Developers: BE THE GUIDE!
- October 2022
-
August 2022
- Aug 23, 2022 Using .addJoinQuery() & How to Query Records with Attachments in ServiceNow
- Aug 18, 2022 Free, Simple URL Shortener for ServiceNow Nerds (snc.guru)
- Aug 16, 2022 How to Get and Parse ServiceNow Journal Entries as Strings/HTML
- Aug 14, 2022 New tool: Get Latest Version of ServiceNow Docs Page
- March 2022
- February 2022
- May 2021
- April 2021
- February 2021
-
November 2020
- Nov 17, 2020 SN Guys is now part of Jahnel Group!
- September 2020
- July 2020
-
January 2020
- Jan 20, 2020 Getting Help from the ServiceNow Community
- December 2019
- November 2019
-
April 2019
- Apr 21, 2019 Understanding Attachments in ServiceNow
- Apr 10, 2019 Using Custom Search Engines in Chrome to Quickly Navigate ServiceNow
- Apr 4, 2019 Set Catalog Variables from URL Params (Free tool)
- Apr 1, 2019 Outlook for Android Breaks Email Approvals (+Solution)
- March 2019
-
February 2019
- Feb 27, 2019 Making Update Sets Smarter - Free Tool
-
November 2018
- Nov 29, 2018 How to Learn ServiceNow
- Nov 6, 2018 ServiceNow & ITSM as a Career?
- October 2018
- September 2018
-
July 2018
- Jul 23, 2018 Admin Duty Separation with a Single Account
-
June 2018
- Jun 19, 2018 Improving Performance on Older Instances with Table Rotation
- Jun 4, 2018 New Free Tool: Login Link Generator
-
May 2018
- May 29, 2018 Learning ServiceNow: Second Edition!
- April 2018
- March 2018
-
February 2018
- Feb 11, 2018 We have a new book!
- November 2017
-
September 2017
- Sep 12, 2017 Handling TimeZones in ServiceNow (TimeZoneUtil)
- July 2017
-
June 2017
- Jun 25, 2017 What's New in ServiceNow: Jakarta (Pt. 1)
- Jun 4, 2017 Powerful Scripted Text Search in ServiceNow
- May 2017
- April 2017
-
March 2017
- Mar 12, 2017 reCAPTCHA in ServiceNow CMS/Service Portal
-
December 2016
- Dec 20, 2016 Pro Tip: Use updateMultiple() for Maximum Efficiency!
- Dec 2, 2016 We're Writing a Book!
-
November 2016
- Nov 10, 2016 Chrome Extension: Load in ServiceNow Frame
- September 2016
-
July 2016
- Jul 17, 2016 Granting Temporary Roles/Groups in ServiceNow
- Jul 15, 2016 Scripted REST APIs & Retrieving RITM Variables via SRAPI
-
May 2016
- May 17, 2016 What's New in Helsinki?
-
April 2016
- Apr 27, 2016 Customizing UI16 Through CSS and System Properties
- Apr 5, 2016 ServiceNow Versions: Express Vs. Enterprise
-
March 2016
- Mar 28, 2016 Update Set Collision Avoidance Tool: V2
- Mar 18, 2016 ServiceNow: What's New in Geneva & UI16 (Pt. 2)
-
February 2016
- Feb 22, 2016 Reference Field Auto-Complete Attributes
- Feb 6, 2016 GlideRecord & GlideAjax: Client-Side Vs. Server-Side
- Feb 1, 2016 Make Your Log Entries Easier to Find
-
January 2016
- Jan 29, 2016 A Better, One-Click Approval
- Jan 25, 2016 Quickly Move Changes Between Update Sets
- Jan 20, 2016 Customize the Reference Icon Pop-up
- Jan 7, 2016 ServiceNow: Geneva & UI16 - What's new
- Jan 4, 2016 Detect/Prevent Update Set Conflicts Before They Happen
-
December 2015
- Dec 28, 2015 SN101: Boolean logic and ServiceNow's Condition Builder
- Dec 17, 2015 Locate any record in any table, by sys_id in ServiceNow
- Dec 16, 2015 Detecting Duplicate Records with GlideAggregate
- Dec 11, 2015 Array.indexOf() not working in ServiceNow - Solution!
- Dec 2, 2015 Understanding Dynamic Filters & Checking a Record Against a Filter Using GlideFilter
- October 2015
-
August 2015
- Aug 27, 2015 Easily Clone One User's Access to Another User