Script to remove model Categories from Hardware Model

eagle18
Tera Contributor

Hey all, i need some asssistance creating a script to update model categories on some hardware models that have the incorrect model categories  captured. 

find_real_file.png

Issue i am tryiing to resolve is most of our EUC devices have been captured as both Computer and Personal Computer Model categoy which is causing inconsistant issues with reporting and ci classes mapping. 

What i'm attemting to do is put togeather a script to remove the computer category from the Model but my scripting skills are curently still limited so unsure on the best approach to do this. 

Appreciate your help

1 ACCEPTED SOLUTION

like this

var sysId = ''; // give sys_id of personal computer category

var gr = new GlideRecord("cmdb_hardware_product_model");
gr.addEncodedQuery("cmdb_model_categoryLIKE" + sysId);
gr.query();
while(gr.next()) {

	var arr = gr.cmdb_model_category.toString().split(',');
	var index = arr.indexOf(sysId);
	if (index > -1) {
		arr.splice(index, 1); // 2nd parameter means remove one item only
	}

	gr.cmdb_model_category = arr.toString();
	gr.setWorkflow(false);
	gr.update();
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you provide details on on which table this is done and what is the field type?

what script you started with?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar 

Its on the cmdb_hardware_product_model tablle looking to update the field cmdb_model_category which is a glide list. 

Is  this the correct approach to run a fix script; 

 

var asset = new GlideRecord('cmdb_hardware_product_model');

var encodedStr = ' string'
asset.addQuery(encodedStr ); //Hardware Asset Models
asset.query();

while (asset.next()) {

asset.setValue = '1234568'; //Model Category = sys_id of 'Personal Computer'

asset.update();

}

like this

var sysId = ''; // give sys_id of personal computer category

var gr = new GlideRecord("cmdb_hardware_product_model");
gr.addEncodedQuery("cmdb_model_categoryLIKE" + sysId);
gr.query();
while(gr.next()) {

	var arr = gr.cmdb_model_category.toString().split(',');
	var index = arr.indexOf(sysId);
	if (index > -1) {
		arr.splice(index, 1); // 2nd parameter means remove one item only
	}

	gr.cmdb_model_category = arr.toString();
	gr.setWorkflow(false);
	gr.update();
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader