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 , Also, records with RETIRED state need not be altered. Is this something that we can add in builder?

@Swetha M you can change the condition to 

data.addEncodedQuery('active=true^install_status!=7^managed_by.active=false^ORowned_by.active=false^ORu_managed_company.active=false^ORowned_byISEMPTY^ORmanaged_byISEMPTY^ORu_managed_companyISEMPTY');

'install_status' is the backend name for the State field and 7 is the choice value for retired state.

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();

}
}