tiagomacul
Giga Sage

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:

  1. Update uppercase first letter
  2. Update Incident state
  3. Close multiple incidents from
  4.  

 

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.

image

summary

Script summary

Scripts - Oficial Doc.

Versões do ServiceNow

updateMultiple()

Close incidents using UI

Comments
lasse3
Mega Guru

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.

Version history
Last update:
‎06-05-2025 10:30 AM
Updated by:
Contributors