How to write a condition that checks if a CI is related with an Offering

Tal5
Giga Guru

Hey experts,

Using Script Include, how do I write a condition that checks whether the CI has a Parent, assuming I'm looking at the cmdb_rel_ci table

 

Thanks in advance

2 REPLIES 2

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Tal5 

 

To check if a Configuration Item (CI) has a parent in the cmdb_rel_ci table using a Script Include in ServiceNow, you can write a condition to query the table and check if there are any records where the CI is the child. Here's how you can do it:

 

var CIUtils = Class.create();
CIUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Check if the CI has a parent
    hasParent: function(ciSysId) {
        var hasParent = false;

        // Query cmdb_rel_ci table to check if the CI has a parent
        var gr = new GlideRecord('cmdb_rel_ci');
        gr.addQuery('child', ciSysId); // Filter by the CI being the child
        gr.query();
        
        if (gr.hasNext()) {
            hasParent = true;
        }

        return hasParent;
    },

    type: 'CIUtils'
});

 

 

You can then call the hasParent function from your script and pass the CI's sys_id as a parameter to determine if it has a parent CI in the cmdb_rel_ci table. For example:

 

 

var ciSysId = 'sys_id_of_your_ci'; // Replace this with the actual sys_id of the CI
var ciUtils = new CIUtils();
var hasParent = ciUtils.hasParent(ciSysId);
gs.info('Does CI have a parent? ' + hasParent);

 

This script will return true if the CI has a parent and false if it doesn't.

 

🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

🙂

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)