Update Reference Field in Script

mdidrikson
Kilo Contributor

Hello,

I would like to update the manufacturer field in the CMDBo to be 'Hewlett-Packard' for all records . This field is a reference field with the following information.

Table: cmdb_ci
Element: manufacturer
Type: reference
Reference: core_company

Here is my script.



var rec = new GlideRecord('cmdb_ci_computer');
rec.query();
while (rec.next()) {
rec.manufacturer = 'Hewlett-Packard'
gs.print('Record Updated ' + rec.manufacturer);
rec.update();
}

gs.print(rec.getRowCount());



Is there a way this update can be run with a script?

Thanks!

Mark Didrikson

5 REPLIES 5

Mark Stanger
Giga Sage

You can run this from the 'Scripts - Background' module after elevating your privileges to 'security_admin'. If you want to set the name in a reference field rather than a sys_id, you'll need to use 'setDisplayValue' like this...



var rec = new GlideRecord('cmdb_ci_computer');
rec.query();
while (rec.next()) {
rec.manufacturer.setDisplayValue('Hewlett-Packard');
gs.print('Record Updated ' + rec.manufacturer);
rec.update();
}

gs.print(rec.getRowCount());


Great info. Thanks Mark.

Community Alums
Not applicable

Hi mark,

 

I used same but the old value getting deleted but not new value updated

could u help me with that.

 

Regards,

Roopa

Community Alums
Not applicable

it worked problem with single quote 🙂

 

Thanks