Automatic associate Asset and CI based on matching serial number

XiaoC
Giga Contributor

Hello Team,

 

I am inquiring about the out-of-the-box functionality in ServiceNow regarding the automatic relationship between records. Specifically, when a new record is created in either the Asset table (alm_hardware) or the CI tables under hardware and extended tables, is there a feature that automatically associates the two records (CI and Asset) based on the serial number? If such functionality exists, could you please provide details or share a link to relevant documentation?

 

Thank you.

1 ACCEPTED SOLUTION

To achieve the functionality where you link an asset to an existing CI based on the serial number, you need to:

  1. Check if a CI with the same serial number exists.
  2. If it exists, link the asset to that CI.
  3. If it doesn’t exist, create a new CI and then link the asset to it.

Here’s how you can modify the code to implement this logic:

try {
    var ci = new GlideRecord('cmdb_ci'); // Replace 'cmdb_ci' with the actual CI table name
    ci.addQuery('serial_number', current.serial_number); // Replace 'serial_number' with the actual field name
    ci.query();
    
    if (ci.next()) {
        // CI with the same serial number exists, so link the asset to this CI
        linkAssetToCI(current, ci); // Implement this function to create the link
    } else {
        // CI with the same serial number does not exist, create a new CI and link the asset
        var assetAndCI = new AssetandCI();
        var newCI = assetAndCI.createCI(current); // This should return the new CI record
        linkAssetToCI(current, newCI); // Implement this function to create the link
    }
} catch (err) {
    gs.addErrorMessage(err);
    current.setAbortAction(true);
}

// Function to link an asset to a CI
function linkAssetToCI(asset, ci) {
    // Example of linking logic, adjust based on your actual linking mechanism
    asset.ci = ci.sys_id; // Replace 'ci' with the field name used to link CI in the asset record
    asset.update();
}

View solution in original post

2 REPLIES 2

HIROSHI SATOH
Mega Sage

There is no functionality for this, so I think it can be achieved by modifying the existing business logic.

「Create CI on insert」

To achieve the functionality where you link an asset to an existing CI based on the serial number, you need to:

  1. Check if a CI with the same serial number exists.
  2. If it exists, link the asset to that CI.
  3. If it doesn’t exist, create a new CI and then link the asset to it.

Here’s how you can modify the code to implement this logic:

try {
    var ci = new GlideRecord('cmdb_ci'); // Replace 'cmdb_ci' with the actual CI table name
    ci.addQuery('serial_number', current.serial_number); // Replace 'serial_number' with the actual field name
    ci.query();
    
    if (ci.next()) {
        // CI with the same serial number exists, so link the asset to this CI
        linkAssetToCI(current, ci); // Implement this function to create the link
    } else {
        // CI with the same serial number does not exist, create a new CI and link the asset
        var assetAndCI = new AssetandCI();
        var newCI = assetAndCI.createCI(current); // This should return the new CI record
        linkAssetToCI(current, newCI); // Implement this function to create the link
    }
} catch (err) {
    gs.addErrorMessage(err);
    current.setAbortAction(true);
}

// Function to link an asset to a CI
function linkAssetToCI(asset, ci) {
    // Example of linking logic, adjust based on your actual linking mechanism
    asset.ci = ci.sys_id; // Replace 'ci' with the field name used to link CI in the asset record
    asset.update();
}