Fix script to update Bulk CIs

Balaji Ramanath
Kilo Contributor

Hi,

I have a requirement to write a fix script to update bulk CI records and update the status to 'Retired'. kindly assist me on fix script for the same. I tried something here below which I would like to run in the background script as it is a bulk number of CI records. Thank you in advance

-----------------------------------------------------------------------------------------

updateRecords();
function updateRecords(){
var gr = new GlideRecord('cmdb_ci');
gr.addEncodedQuery('My Query Here');
gr.update.setlimit(10);//Set limit to test the logic on few records
gr.query();
while(gr.next()){
gr.setValue('status','6');
gr.setWorkflow(false);
gr.update();
}
}

-----------------------------------------------------------------------------------------

1 ACCEPTED SOLUTION

Another way as mentioned in the following NOW Support article is to use .updateMultiple(). Shouldn't use this with .setLimit(). It may slow down the instance during execution.

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0826303

var gr = new GlideRecord("cmdb_ci");
gr.addEncodedQuery('My Query here'); 
gr.setValue('install_status', 4);
gr.setWorkflow(false); // Bypass business rules and workflows
gr.autoSysFields(false);  // Do not update sys_updated_by, sys_updated_on, sys_mod_count, sys_created_by, and sys_created_on
gr.updateMultiple();

View solution in original post

5 REPLIES 5

Ravi9
ServiceNow Employee
ServiceNow Employee

it should be below and not what u wrote , there was a typo i presume , irrespective of that what r u struggling with ? any specific area ? how many actual records r u trying to update ? 

updateRecords();
function updateRecords() {
  var gr = new GlideRecord("cmdb_ci");
  gr.addEncodedQuery("My Query Here");
  gr.setlimit(10); //Set limit to test the logic on few records
  gr.query();
  while (gr.next()) {
    gr.setValue("status", "6");
    gr.setWorkflow(false);
    gr.update();
  }
}

Ravi - thank you. We are trying to update the status of 100k+ CI records. 

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Balaji,

"status" is a label. It's necessary to specify column name. The name of the column corresponding to label "Status" is "install_status". It's also of type Integer and not String.

Also, note that the script will only update records directly in the cmdb_ci table and not those in table that inherits the cmdb_ci table.

function updateRecords() {
    var gr = new GlideRecord('cmdb_ci');
    gr.addEncodedQuery('My Query Here');
    gr.update.setlimit(10); //Set limit to test the logic on few records
    gr.query();
    while (gr.next()) {
        gr.setValue('install_status', 6);
        gr.setWorkflow(false);
        gr.update();
    }
}
updateRecords();

 

Noted with thanks Hitoshi. Yes, trying to update the records only on the cmdb_ci table.