If the relationships in your cmdb_rel_ci table don't directly use 'parent' and 'child' fields as I previously assumed, it's likely that they're using a different schema to relate CIs together, typically via parent and child references found in other tables such as cmdb_ci_rel which specifically tracks relationships between Configuration Items (CIs). Let's correct the approach accordingly.
Adjusting for Actual Relationship Schema
In ServiceNow, the typical way to represent relationships between CIs such as servers and their related CIs would use the cmdb_rel_ci table (or similarly named, based on version and customization, often it's cmdb_ci_rel). This table tracks relationships and might include fields like parent, child, and often a type to indicate the nature of the relationship.
Given this, we need to adjust our approach to correctly query these relationships. Assuming now a more standard approach:
- Identify the Server IP: You've selected a server in the Catalog item, and you want to find other CIs that are related to this server based on the server's IP address.
- Find Related CIs: You'll need to understand how relationships are defined. If using
cmdb_ci_rel, relationships are usually marked with a parent and child sys_id, and potentially a type_id to define the relationship nature.
Updated Server-side Script
Given the absence of specific 'parent' and 'child' IP fields, let's focus on fetching the related CIs via a different method. Assuming the cmdb_ci_server records are related to other CIs through cmdb_ci_rel:
var CheckOperationalStatus = Class.create();
CheckOperationalStatus.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getRelatedCIs: function() {
var serverId = this.getParameter('sysparm_server_id');
var ipAddresses = [];
// Assume relationships are stored in 'cmdb_ci_rel'
// We'll check both where the server is a parent and where it's a child, for completeness
var relGr = new GlideRecord('cmdb_ci_rel');
relGr.addQuery('parent', serverId);
relGr.addOrCondition('child', serverId);
relGr.query();
while (relGr.next()) {
var childId = relGr.child == serverId ? relGr.parent : relGr.child; // Determines the related CI
var childGr = new GlideRecord('cmdb_ci'); // General CI table, adjust if you know exact type
if (childGr.get(childId) && childGr.operational_status == '1') {
// Assuming related CI also has 'ip_address' field, adjust field name as necessary
ipAddresses.push(childGr.getValue('ip_address'));
}
}
return ipAddresses.join("\n");
},
type: 'CheckOperationalStatus'
});
Key Adjustments:
- Relationship Query: The corrected script queries
cmdb_ci_rel, adjusting for the relationship between parent and child CIs. This approach reads both cases where the selected server could be either a parent or a child in relationships, as real-world setups can have complex relationships.
- IP Address and Operational Status Logic: We're now focusing on any CI related to the server (not strictly child or parent by IP), checking its operational status, and then listing IP addresses of those CIs.
Next Steps:
Following the instructions for creating an onChange Client Script from the previous reply should still apply, as that segment primarily handles invoking this server-side logic and displaying results.
Caution:
- Performance: Be cautious about performance and access control, especially when querying and processing potentially large sets of related CIs.
- Schema Assumptions: This example assumes a simplistic schema. Your actual schema for CIs and relationships may differ, requiring adjustments to field names and logic.
This revised solution should more accurately fit a standard ServiceNow instance's manner of tracking CI relationships, but ensure to adjust for your specific schema and requirements.