Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Update Model Category on more than one asset

tpalamore
Tera Contributor

Is there a script to update the model category on multiple assets at once?

 

I have a script for single assets, but I currently need to update the model category on over 200 assets.

 

This is the script that works for updating the model category on individual assets from Computer to Mobile Device:

 

var mobiledeviceModelClass = 'sys_id';
var assetId = 'sys_id';

var grHardware = new GlideRecord('alm_asset');
grHardware.addQuery('sys_id', assetId);
grHardware.query();
if (grHardware.next()) {
  grHardware.model_category = mobiledeviceModelClass;
  grHardware.update();
  gs.info("updated");
} else {
  gs.info("skipped");
}

 

 

Any input would be helpful.

Thank you.

 

1 ACCEPTED SOLUTION

Brad Bowman
Mega Patron

If you only have/can get the sys_id of each asset, you could paste those in a comma-separated string to assign to assetId, then change the rest of the script to be more like this:

var grHardware = new GlideRecord('alm_asset');
grHardware.addQuery('sys_id', 'IN', assetId);
grHardware.query();
while (grHardware.next()) {
  grHardware.model_category = mobiledeviceModelClass;
  grHardware.update();
  gs.info("updated");
} else {
  gs.info("skipped");
}

 An alternate approach is to use a System Data Management > Update Job in the left nav.  You can identify the target assets based on a condition, or paste in your list of sys_ids, preview to make sure the count is right, then set the action in a no-code UI. 

View solution in original post

3 REPLIES 3

Brad Bowman
Mega Patron

If you only have/can get the sys_id of each asset, you could paste those in a comma-separated string to assign to assetId, then change the rest of the script to be more like this:

var grHardware = new GlideRecord('alm_asset');
grHardware.addQuery('sys_id', 'IN', assetId);
grHardware.query();
while (grHardware.next()) {
  grHardware.model_category = mobiledeviceModelClass;
  grHardware.update();
  gs.info("updated");
} else {
  gs.info("skipped");
}

 An alternate approach is to use a System Data Management > Update Job in the left nav.  You can identify the target assets based on a condition, or paste in your list of sys_ids, preview to make sure the count is right, then set the action in a no-code UI. 

Your script worked. I had to take the "} else {" off the end of the script.

 

Thank you!

Script that was used for reference:

 

var mobiledeviceModelClass = '0be11f58c7432010f74c784c95c260b7';
var assetId = [
    '00b0f4af1b7d1190b570b997cc4bcb0f',
    '0a500ee5977071100ce39614a253af20',
    'c0fd38081ba3e510b570b997cc4bcb92',
    'a9c6001597390910207bf5faf253aff0',
    'ec023beb1b6d6110b570b997cc4bcb07'
];

var grHardware = new GlideRecord('alm_asset');
grHardware.addQuery('sys_id', 'IN', assetId);
grHardware.query();
while (grHardware.next()) {
  grHardware.model_category = mobiledeviceModelClass;
  grHardware.update();
  gs.info("updated");
  gs.info("skipped");
}