Swap CIs that is related to an Asset

Henrik Jutterst
Tera Guru

Hi all!

I have a set of hardware assets located in alm_hardware that I need to change the related CI of an Asset to another CI. Basically the CI that is related to the asset now is the wrong one and I'm looking for a way to script this change.

Now it loks like this:

Asset tagRelated CI
A1C1
A2C2
A3C3
......

This is what I'm looking for:

Asset tagNew Related CIOld Related CI
A1New C1C1
A2New C2C2
A3New C2C2
.........

Basically I want to insert a new CI related to the Asset just like Assets and CI's are linked together. And then add a CI relation between "New Related CI" and "Old Related CI" in the cmdb_rel_ci table.

Can someone with a bit of scripting knowledge show me what a script would look like?

I'm looking to send in the "Asset tag" and the "New Related CI" in a set of data

Kind Regards

/Henrik

1 ACCEPTED SOLUTION

Hmm.. this was some time ago, but I think I solved it with this script:

 

// link CI to Asset just like Assets are linked to CIs
var gr = new GlideRecord('alm_asset'); //create glide record on table
gr.query();	//query all records in table

while(gr.next()) {
   var Id = gr.ci;	//get sys_id for CI in alm_asset table
   var cmd = new GlideRecord('cmdb_ci'); //create glide record on table

   cmd.addQuery('sys_id',Id); //where sys_id from cmdb_ci == sys_id from ci on Asset table (Id)
   cmd.query(); // run query

   if(cmd.next()) {
      cmd.asset = gr.sys_id; //update asset field on cmdb_ci table with 
      cmd.setWorkflow(false); //disable running of business rules that might normally be triggered by subsequent actions
      cmd.update(); //commit update
   }
}

View solution in original post

3 REPLIES 3

Henrik Jutterst
Tera Guru

Anyone with ideas?


Jason Roiz
Kilo Contributor

I need to do something very similar. I need a script that would move a relationship from one asset to another and then delete the old asset.

I am already finding duplicates on serial number in alm_hardware with a script. Now I need to act on them based on one asset having a related CI and the other asset not having a related CI. Then move the CI relationship to the latter.

Hmm.. this was some time ago, but I think I solved it with this script:

 

// link CI to Asset just like Assets are linked to CIs
var gr = new GlideRecord('alm_asset'); //create glide record on table
gr.query();	//query all records in table

while(gr.next()) {
   var Id = gr.ci;	//get sys_id for CI in alm_asset table
   var cmd = new GlideRecord('cmdb_ci'); //create glide record on table

   cmd.addQuery('sys_id',Id); //where sys_id from cmdb_ci == sys_id from ci on Asset table (Id)
   cmd.query(); // run query

   if(cmd.next()) {
      cmd.asset = gr.sys_id; //update asset field on cmdb_ci table with 
      cmd.setWorkflow(false); //disable running of business rules that might normally be triggered by subsequent actions
      cmd.update(); //commit update
   }
}