Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Asset and CI inconsistency

Ashwini101
Tera Contributor

I have a requirement where There are many cases where for one asset their multiple CI records are attached - the relationships seem to be invalid.

Ashwini101_0-1762424517195.png

My requirement is:

  1. First fetch the records which are having asset record sysid
  2. verify the duplicate each record from cmdb table with (alm_asset, alm_hardware) tables field are once got the correct match record.
  • cmdb_ci table - serial_number, asset_tag, model_id
  • alm_asset table - serial_number, asset_tag, model
  • If matches the records both in alm_hardware and cmdb_ci then unlink the records and push them to correct CI record with correct asset tag
  • once done change those all display_name to (serial_number + ' - ' + model) as display_name in alm_hardware and alm_asset table.
  • Then update the same asset display_name sysid record to respective CI asset record.
  • CI should be visible in the related list of asset record.

this highlighted point is not working as expected when I use below script

 

(function executeFixScript() {
    var assetGR = new GlideRecord('alm_hardware');
    assetGR.query();

    while (assetGR.next()) {
        var ciGR = new GlideRecord('cmdb_ci');
        ciGR.addQuery('asset', assetGR.sys_id);
        ciGR.query();

        var ciList = [];
        while (ciGR.next()) {
            ciList.push(ciGR.sys_id.toString());
        }

        // If more than one CI is linked, unlink all except the first
        if (ciList.length > 1) {
            for (var i = 1; i < ciList.length; i++) {
                var ciToUnlink = new GlideRecord('cmdb_ci');
                if (ciToUnlink.get(ciList[i])) {
                    ciToUnlink.asset = '';
                    ciToUnlink.update();
                    gs.info('Unlinked CI: ' + ciToUnlink.name + ' from Asset: ' + assetGR.name);
                }
            }
        }

        // Update display_name as "serial_number - model"
        var newDisplayName = (assetGR.serial_number || '') + ' - ' + (assetGR.model || '');
        assetGR.display_name = newDisplayName;
        assetGR.update();
        gs.info('Updated display_name for Asset: ' + assetGR.name + ' to ' + newDisplayName);
    }
})();
 
 
#cmdb #asset #CI &Asset
0 REPLIES 0