Create link from CI to Asset

Henrik Jutterst
Tera Guru

Can someone please help me here?

We have manually imported CI and Assets into CMDB. This was done at two separate occasions. First we imported the CIs without relation to an Asset and then we imported the Assets that had relation to CIs.

This resulted in that I can see relating CI from an Asset, but not relating Asset from a CI. Now I need to relate the ~10 000 CIs with the once that have an Asset. How can this be done?

Anyone that can help out with a background script on this?

I did take a look on this page:

Asset and CI management

I know there are some business rules that run in the background when creating Assets or CI.

  • Update CI fields on change (on the Asset [alm_asset] table)
  • Update Assets on change (on the Configuration Item [cmdb_ci] table)
1 ACCEPTED SOLUTION

HI Henrik,



Now what you have to do is write a one time script.



You will have to query on asset able and take value of CI field and then query CI table and update Asset field on CI Table.



See below script:



var gr = new GlideRecord('alm_asset');


gr.query();


while(gr.next())


{


var Id = gr.ci;



var cmd = new GlideRecord('cmdb_ci');


cmd.addQuery('sys_id',Id);


cmd.query();


if(cmd.next())


{


cmd.asset = gr.sys_id;


cmd.setWorkflow(false);


cmd.update();


}


}



Thank you,


Ashutosh


View solution in original post

9 REPLIES 9

HI Henrik,



Now what you have to do is write a one time script.



You will have to query on asset able and take value of CI field and then query CI table and update Asset field on CI Table.



See below script:



var gr = new GlideRecord('alm_asset');


gr.query();


while(gr.next())


{


var Id = gr.ci;



var cmd = new GlideRecord('cmdb_ci');


cmd.addQuery('sys_id',Id);


cmd.query();


if(cmd.next())


{


cmd.asset = gr.sys_id;


cmd.setWorkflow(false);


cmd.update();


}


}



Thank you,


Ashutosh


It worked! 😄 Just what I was looking for. Thank you so much. Now I can see the Asset relation from the CI to the Asset!



Have a great day.


Great


Same code just with comments for each step to explain.



// 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


    }


}


Hi Henrik,



If you want to Just update one record initially for test you can do that by adding additional query at line 3 as


gr.addQuery('sys_id', "SYS ID OF ASSET Record");



Thank you,
Ashutosh Munot