The CreatorCon Call for Content is officially open! Get started here.

Fix Script to change sys ids of Asset and CI

san1989
Giga Guru

Hello All,

One asset is associated with one CI, but I have another CI which has to be associated to this asset.

For example: I have Asset "XYZ" and it has related CI "XYZ". But there is another CI "ABC". 

Now this Asset "XYZ" has to be related to CI "ABC". 

My requirement is, I should keep the sys_id of Asset XYZ in the asset field of CI ABC and should remove the sys_id of asset xyz from the CI XYZ.

Can anyone please help with script in fix script.

Thanks in advance.

 

3 REPLIES 3

Chuck Tomasi
Tera Patron

It would go something like this:

Standard disclaimer: The following code is untested, requires review and potential modifications.

(function () {  

    // Clear out old asset value of CI XYZ
    var ci1 = new GlideRecord('cmdb_ci');

    if (ci1.get('asset', 'SYS_ID_OF_ASSET_XYZ')) {
        ci1.asset = '';
        ci1.update();
    }
 
    // Set the value of the new GlideRecord
    var ci2 = new GlideRecord('cmdb_ci');

    if (ci2.get('SYS_ID_OF_CI_ABC')) {
        ci2.asset = 'SYS_ID_OF_ASSET_XYZ';
        ci2.update();
    }
})();  

Thanks for your help, what would be the best practice if  I want change sys_id's for more records at least 50 records.

Thanks

Use an array or query for the records in a loop, then iterate over each record. It sounds like you need to know three bits of information:

  • Original CI
  • New CI
  • Asset

That implies you have a list somewhere of those elements or you can query for them. While it's easy to query for the CI that currently as the asset, I don't yet understand how you would get a list of New CIs that you want to assign the asset to.