logo

NJP

UpdateMultiple Operation - optimizing your code

Import · Jan 18, 2020 · article

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

Example: Update Incident state

var gr = new GlideRecord('incident')

gr.addQuery('number','IN', 'INC0019002,INC0017892,INC0017869');

gr.query();

gr.setValue('state', 8);

gr.updateMultiple(); 

The following example adds the string "Updated" to the name of the first 100 customer names that start with S.

var gr = new GlideRecord('sys_user')

gr.addQuery('name','STARTSWITH', 's');

gr.query();

gr.setValue('first_name', gs.first_name + '  updated');

gr.updateMultiple(); 

update incident state

closeStaleIncidents();
function closeStaleIncidents() {
    var staleState = 8;
    var query = 'sys_updated_onRELATIVELT@dayofweek@ago@3^state=2';

    var gr = new GlideRecord('incident');
    gr.addQuery(query);
    gr.setValue('state', staleState);
    gr.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

View original source

https://www.servicenow.com/community/developer-articles/updatemultiple-operation-optimizing-your-code/ta-p/2323884