
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
01-18-2020 05:33 PM - edited 06-05-2025 10:30 AM
UpdateMultiple Operation - optimizing your code, a better option to codify.
"Both update() and updateMultiple() will result in a database update. The main difference is that update() operates on one record at a time, whereas updateMultiple() updates any and all records returned from your GlideRecord query, without using next() to iterate through each one. update() returns the Sys ID of the updated record. update() accepts one optional argument: the reason for the update. This reason will be logged in the audit history for the updated record. updateMultiple() on the other hand, accepts no arguments, and returns no value." oreilly
Usage Example:
- Update uppercase first letter
- Update Incident state
- Close multiple incidents from
Example: Update Incident state
var grInc = new GlideRecord('incident')
grInc.addQuery('number','IN', 'INC0019002,INC0017892,INC0017869');
grInc.query();
grInc.setValue('state', 8);
grInc.updateMultiple();
The following example adds the string "Updated" to the name of the first 100 customer names that start with S.
var grUser = new GlideRecord('sys_user')
grUser.addQuery('name','STARTSWITH', 's');
grUser.query();
grUser.setValue('first_name', gs.first_name + ' updated');
grUser.updateMultiple();
update incident state
closeStaleIncidents();
function closeStaleIncidents() {
var staleState = 8;
var query = 'sys_updated_onRELATIVELT@dayofweek@ago@3^state=2';
var grInc = new GlideRecord('incident');
grInc.addQuery(query);
grInc.setValue('state', staleState);
grInc.updateMultiple();
}
Do not use this method with the chooseWindow() or setLimit() methods when working with large tables.
- 25,593 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This is a great guide, thank you for this!
For performance reasons there is however no need to use gr.query() as this query is never actually used anywhere. You do not need to query before using updateMultiple().
So the first script should like this instead:
var gr = new GlideRecord('incident')
gr.addQuery('number','IN', 'INC0019002,INC0017892,INC0017869');
gr.setValue('state', 8);
gr.updateMultiple();
Just wanted to update this article as I see that many people are missing this point.