The CreatorCon Call for Content is officially open! Get started here.

How to find the parent server of the child server using script include

Anna_Servicenow
Tera Guru

>I need to glide cmdb_rel_ci table and need to get the names of parents related to that child.

Anna_Servicenow_0-1690303170865.png

> if child has two parent, I need to check if one of the parent has citrix in name.

How to achieve this

4 REPLIES 4

pratiksha5
Mega Sage

Sharing a background script. Modify it accordingly

 

// Define the child's sys_id for which you want to find the parent names
var childSysID = '<INSERT_CHILD_SYS_ID_HERE>';

// GlideRecord query to fetch relationships for the given child
var relationshipGR = new GlideRecord('cmdb_rel_ci');
relationshipGR.addQuery('child', childSysID);
relationshipGR.query();

// Array to store parent names that contain 'citrix'
var parentNamesWithCitrix = [];

// Loop through the relationships and find parents with 'citrix' in the name
while (relationshipGR.next()) {
var parentSysID = relationshipGR.parent.toString();

// GlideRecord query to fetch parent details
var parentGR = new GlideRecord('cmdb_ci_rel');
parentGR.get(parentSysID);

// Check if the parent name contains 'citrix' and add it to the array if true
if (parentGR.name.toString().toLowerCase().indexOf('citrix') !== -1) {
parentNamesWithCitrix.push(parentGR.name.toString());
}
}

// Print the names of parents with 'citrix' in the name
gs.info("Parents with 'citrix' in the name for child with sys_id " + childSysID + ":");
for (var i = 0; i < parentNamesWithCitrix.length; i++) {
gs.info(parentNamesWithCitrix[i]);
}

Thank you!!
Please help me with the script on how i can pass 3 sys_id  (meaning 3 child servers)  and also I need the count of child servers that doesn't have "citrix" in any of the parent 

emily1
Tera Guru

hi! here is a background script you can run to search for three child CIs and return the results you need:

//change each value between the commas in variable to equal the sys IDs of the server you want to check the parents of
//make sure to leave the spacing as is and do not remove the quotations
var childID = 'sysid1,sysid2,sysID3';

//do not change anything else below this line
var count = 0;
var citrixParent = '';
var citrixList = [];

//query the relationship table - you do not need to edit this part
var rel = new GlideRecord('cmdb_rel_ci');
rel.addEncodedQuery('child.sys_idIN' + childID);
rel.query();
//loop through the matches and pull out parents with citrix in the name
while (rel.next()) {
    var parentName = rel.parent.name;
   // gs.info(parentName);
    if (parentName.indexOf('citrix') > -1 || parentName.indexOf('Citrix') > -1) {
        citrixParent = parentName;
        citrixList.push(citrixParent);
        //gs.info(citrixParent);
    } else {
        count += count + 1;
    }

}
gs.info(citrixList);
gs.info('there are ' + count + ' child records that do not have a parent record with Citrix in the name');

 

if you need this to be more specific, please let me know.

 

the results should return the names of the severs like this:

*** Script: CitrixParent1,CitrixParent2
*** Script: there are 7 child records that do not have a parent record with Citrix in the name

tried this with the same screenshot example.. but the count is returned as 3. It should be 1 as one child has parent that doesn't have citrix in either parent name.

 

Also where exactly in thecode are we checking if one of the parent name has citrix?