Way to populate List of related CI

suyoga
Tera Expert

I have a requirement to have a UI action on incident table Affected CI related list which should popup ui page and populate the list of CI's that are related to the configuration item of the incident, currently I have created the logic where I am passing the value on cmdb_rel_ci and then populating the name in custom UI page but its not efficient way as cmdb_rel_ci have huge data, so its taking lot of time populate the list, can somebody please help me with above reuqirement if there is any OOTB method already available or any simpler and efficient way to achieve it

 

var ciSysId = this.getParameter('sysparm_ci_sys_id');
var relatedCIs = []; // Final array to return

if (!ciSysId) {
gs.warn("CIRelationshipProcessor.getRelatedCIs: No CI Sys ID provided.");
return JSON.stringify([]);
}

// Phase 1: Collect unique related CI sys_ids and initial relationship data
var relationshipsData = []; // To store simplified relationship info
var uniqueRelatedCISysIds = new GlideSet(); // Use GlideSet for efficient uniqueness checking

var grRel = new GlideRecord('cmdb_rel_ci');
 
grRel.addQuery('parent', ciSysId);
grRel.query();

while (grRel.next()) {
var relatedCIid = grRel.child + ''; // The related CI is always the child
var relationshipType = grRel.type.child_label.getDisplayValue(); // Use the child_label for description
if (!relationshipType) {
// Fallback if child_label is not set
relationshipType = grRel.type.getDisplayValue() + ' ' + grRel.child.name.getDisplayValue();
}

if (relatedCIid) { // Ensure relatedCIid is not empty
uniqueRelatedCISysIds.add(relatedCIid); // Add to set for uniqueness
relationshipsData.push({
ci_sys_id: relatedCIid,
relationship_type: relationshipType
});
}
}

// Phase 2: Fetch all necessary CI details in a single query
var ciDetailsMap = {}; // Map to store CI details by sys_id
if (!uniqueRelatedCISysIds.isEmpty()) {
var grCIs = new GlideRecord('cmdb_ci');
grCIs.addQuery('sys_id', 'IN', uniqueRelatedCISysIds.toArray().join(',')); // Efficiently query all unique CIs
grCIs.query();

while (grCIs.next()) {
ciDetailsMap[grCIs.sys_id + ''] = {
name: grCIs.name + '',
ci_class: grCIs.getDisplayValue('sys_class_name')
};
}
}

// Phase 3: Construct the final array using fetched details
 
// The GlideSet ensures unique CI objects, but this loop recreates them based on relationshipsData.
// If strict uniqueness of the *displayed CI* (not relationship) is needed, you could iterate
// uniqueRelatedCISysIds.toArray() instead and look up in ciDetailsMap, but this would lose
// relationshipType uniqueness if multiple relationship types lead to the same child.
// For this requirement, iterating relationshipsData is fine.
for (var i = 0; i < relationshipsData.length; i++) {
var rel = relationshipsData[i];
var ciDetails = ciDetailsMap[rel.ci_sys_id];

// Only add if CI details were successfully fetched (CI exists)
if (ciDetails) {
relatedCIs.push({
sys_id: rel.ci_sys_id,
name: ciDetails.name,
ci_class: ciDetails.ci_class,
relationship_type: rel.relationship_type
});
}
}

return JSON.stringify(relatedCIs);
1 REPLY 1

Its_Azar
Tera Guru
Tera Guru

Hi there @suyoga 

There isn’t really no OOTB “show related CIs” popup, but you can do via your script by combining parent/child queries, bulk fetching, and possibly leveraging the CMDB Relationship API.

 

  • Query both parent and child in a single go with grRel.addQuery('parent', ciSysId).addOrCondition('child', ciSysId) so you cover both directions.

  • Use GlideAggregate or GlideQuery to fetch unique sys_ids directly, then bulk fetch from cmdb_ci (like you’re already doing with IN).

  • Keep your UI page only for display; do all heavy lifting in a Script Include that the UI Action calls with GlideAjax — that way it’s reusable and easier to maintain.

  • If performance is still an issue, consider CMDB Relationship API (/api/now/cmdb/relationship) which is more optimized for fetching related CIs.

Hope this helps.

 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG