CI relationship for Rack and Network Device

Andreas Cierpka
Kilo Guru

Our network devices are globally distributed (on premises and rented locations). The network engineers are ordering these devices mostly locally and open the discovery via a catalog item. 

By discovery the network device is added into the CMDB.

The location itself must be added manually. For the location we are using the cmn_location class for city, building and room.

We use the cmdb_ci_rack class to define the rack and rack location.

I want to establish a CI relationship of the network device i.e. IP Switch and the Rack. There is an article available
Create or edit a CI relationship but we do have hundreds of devices and I don't want to do this manually. How can I do this in a programmatic way?

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Andreas Cierpka ,

 

You can use below script and run using fix script or background script.

// Function to create a CI relationship
function createRelationship(sourceCI, targetCI, relationshipType) {
    var grRel = new GlideRecord('cmdb_rel_ci');
    grRel.initialize();
    grRel.setValue('parent', sourceCI); // Source CI sys_id
    grRel.setValue('child', targetCI); // Target CI sys_id
    grRel.setValue('type', relationshipType); // Relationship Type sys_id
    grRel.insert();
}

// Example: Map of devices to racks (replace this with your actual data source)
var deviceToRackMap = [
    { deviceName: 'Switch01', rackName: 'Rack01' },
    { deviceName: 'Switch02', rackName: 'Rack02' },
    { deviceName: 'Switch03', rackName: 'Rack03' }
];

// Relationship Type (e.g., "Installs in::Installed in")
var relationshipTypeName = 'Installs in::Installed in'; // Adjust based on your setup

// Fetch relationship type sys_id
var relType = new GlideRecord('cmdb_rel_type');
relType.addQuery('name', relationshipTypeName);
relType.query();
if (!relType.next()) {
    gs.error('Relationship type not found: ' + relationshipTypeName);
} else {
    var relationshipTypeSysId = relType.getUniqueValue();

    // Iterate through the map and create relationships
    for (var i = 0; i < deviceToRackMap.length; i++) {
        var device = deviceToRackMap[i];

        // Fetch network device sys_id
        var deviceGR = new GlideRecord('cmdb_ci_ip_switch');
        deviceGR.addQuery('name', device.deviceName);
        deviceGR.query();
        if (!deviceGR.next()) {
            gs.error('Device not found: ' + device.deviceName);
            continue;
        }

        // Fetch rack sys_id
        var rackGR = new GlideRecord('cmdb_ci_rack');
        rackGR.addQuery('name', device.rackName);
        rackGR.query();
        if (!rackGR.next()) {
            gs.error('Rack not found: ' + device.rackName);
            continue;
        }

        // Create the relationship
        createRelationship(deviceGR.getUniqueValue(), rackGR.getUniqueValue(), relationshipTypeSysId);
        gs.info('Relationship created between ' + device.deviceName + ' and ' + device.rackName);
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

1 REPLY 1

Runjay Patel
Giga Sage

Hi @Andreas Cierpka ,

 

You can use below script and run using fix script or background script.

// Function to create a CI relationship
function createRelationship(sourceCI, targetCI, relationshipType) {
    var grRel = new GlideRecord('cmdb_rel_ci');
    grRel.initialize();
    grRel.setValue('parent', sourceCI); // Source CI sys_id
    grRel.setValue('child', targetCI); // Target CI sys_id
    grRel.setValue('type', relationshipType); // Relationship Type sys_id
    grRel.insert();
}

// Example: Map of devices to racks (replace this with your actual data source)
var deviceToRackMap = [
    { deviceName: 'Switch01', rackName: 'Rack01' },
    { deviceName: 'Switch02', rackName: 'Rack02' },
    { deviceName: 'Switch03', rackName: 'Rack03' }
];

// Relationship Type (e.g., "Installs in::Installed in")
var relationshipTypeName = 'Installs in::Installed in'; // Adjust based on your setup

// Fetch relationship type sys_id
var relType = new GlideRecord('cmdb_rel_type');
relType.addQuery('name', relationshipTypeName);
relType.query();
if (!relType.next()) {
    gs.error('Relationship type not found: ' + relationshipTypeName);
} else {
    var relationshipTypeSysId = relType.getUniqueValue();

    // Iterate through the map and create relationships
    for (var i = 0; i < deviceToRackMap.length; i++) {
        var device = deviceToRackMap[i];

        // Fetch network device sys_id
        var deviceGR = new GlideRecord('cmdb_ci_ip_switch');
        deviceGR.addQuery('name', device.deviceName);
        deviceGR.query();
        if (!deviceGR.next()) {
            gs.error('Device not found: ' + device.deviceName);
            continue;
        }

        // Fetch rack sys_id
        var rackGR = new GlideRecord('cmdb_ci_rack');
        rackGR.addQuery('name', device.rackName);
        rackGR.query();
        if (!rackGR.next()) {
            gs.error('Rack not found: ' + device.rackName);
            continue;
        }

        // Create the relationship
        createRelationship(deviceGR.getUniqueValue(), rackGR.getUniqueValue(), relationshipTypeSysId);
        gs.info('Relationship created between ' + device.deviceName + ' and ' + device.rackName);
    }
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------