How to write a condition that checks if a CI is related with an Offering
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2024 11:15 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 12:56 AM
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.
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2024 08:40 AM
🙂