Update the "managed by" and "owned by" fields for existing hardware records

Swetha M
Mega Guru

The requirement is to update the "managed by" and "owned by" fields for existing hardware records, to correct for empty fields and records where the owner or manager is no longer with the company. Also, records with RETIRED state need not be altered.

 

My BR for new records:

 

getFields();

function getFields(){
var data = new GlideRecord('ci_data_lookup');
data.addQuery('active','true');
data.addQuery('ci_class',current.model_category.cmdb_ci_class);
data.query();
//gs.addInfoMessage(data.getRowCount());
while(data.next()){
current.managed_by = data.u_managed_by;
current.owned_by = data.u_owned_by;
current.u_managed_company = data.u_managed_company;
current.update();
}
}

 

BR would run and work for new records.

How and what can I do to update the existing records?

1 ACCEPTED SOLUTION

Hi @nitinsharma2510 ,

 

I did the below changes in code and it worked perfectly fine.

var data = new GlideRecord('alm_hardware');
data.addEncodedQuery('install_status!=7^ORinstall_status=NULL');

//data.setLimit(2);
data.query();

while(data.next()){

var lookupData = new GlideRecord('ci_data_lookup');

lookupData.addActiveQuery();
    lookupData.addQuery('ci_class',data.model_category.cmdb_ci_class);

lookupData.query();

if(lookupData.next()){
    data.managed_by = lookupData.u_managed_by;
    data.owned_by = lookupData.u_owned_by;
    data.u_managed_company = lookupData.u_managed_company;
    data.update();

}
}

View solution in original post

7 REPLIES 7

nitinsharma2510
Giga Guru

Hey @Swetha M,

You can create a fix script or run this code in a background script.

var data = new GlideRecord('<Table name to be corrected>');
data.addEncodedQuery('active=true^managed_by.active=false^ORowned_by.active=false^ORu_managed_company.active=false^ORowned_byISEMPTY^ORmanaged_byISEMPTY^ORu_managed_companyISEMPTY');
data.query();
var lookupData = new GlideRecord('ci_data_lookup');
while(data.next()){
	lookupData.addQuery('ci_class',data.model_category.cmdb_ci_class);
	data.managed_by = data.u_managed_by;
	data.owned_by = data.u_owned_by;
	data.u_managed_company = data.u_managed_company;
	data.update();
}

The encoded query can be updated accordingly to match your requirement. Also, I would suggest you do an impact analysis before running this script in the Production (can be tested in lower instances).

Thank you,

Nitin Sharma

Hi @nitinsharma2510 ,

 

The table name to be corrected is my Hardware table i suppose.?. because I wrote that BR on Hardware asset table as those records have to be updated. 

Hey @Swetha M ,

In that case the backend name that needs to be populated in the script in line 1 should be 'cmdb_ci_hardware', unless it is a custom table created by you/your organization.

Ah! @nitinsharma2510 ,

 

It is alm_hardware table . The actual hardware asset table. Will try and let you know..