CI Relationship-Query to get the root CI

Abilash Vangara
Mega Expert


Hi,

 

How can a record in CI Relationships(cmdb_rel_ci) table can be queried to get the root CI to which all these sub CI's are related to. When I mean sub CI that can be at any level, i.e it can be a direct child to the root CI or nested child CI at 3rd or 4th level. No matter at any level or stage the CI is related to the parent CI I need the list of all the parent CI's till the root CI.

 

For more clear explanation refer to the screen shot below

CI Realtionship.PNG

 

Please share your Ideas....!

1 ACCEPTED SOLUTION


Hello Mathan,



I made the below changes in one of the function definitions on the script include (CiUtils2) defined by Mark Stanger(above link)



getCIXML: function(id) {


            var gr = new GlideRecord('cmdb_rel_ci');


            gr.addQuery('child', id);


            gr.query();


            gr.next();


            var str = '';


            //str += '<CI>';


          // str += '<sys_id>'+gr.child.sys_id+'</sys_id>';


            str += gr.child.name+'--';


  //str += '<name>'+gr.child.name+'--'+'</name>';


            //str += '<relType> SELF<br></relType>';


            ret = this._recurs(id);


            if(ret){


                // str += '<children>';


                  str += ret;


                // str += '</children>';


            }


           


            //str += '</CI>';


            return str;


      },



Then created a business rule to call the above function



var ciu = new CIUtils2();


var myXML = ciu.getCIXML(current.cmdb_ci);


var a=myXML.split("--");


for (var i=0; i<a.length; i++)


     


      {


              gs.addInfoMessage(a[i]);


      }



This BR displays all the related CI's of that in the cmdb_ci field...


View solution in original post

12 REPLIES 12

Mark Stanger
Giga Sage

This won't get a single, main parent CI, but it does traverse the full tree and allow you to return parent CIs of any class.   If you wanted the main CI, you could simply look at the returned list and parse out the last CI returned.



» Walking the ServiceNow CMDB Relationship Tree


Thank you Mark for your quick reply ..... The solutions seems to be great


This query var myXML = ciu.getCIXML(current.cmdb_ci); does returns the list of the CI's along with the sys_id's ... as you mentioned about parsing the CI, I tried doing with a split() functionality but couldn't achieve it...can you show me a way to pop out only the CI names one by one?


Hi Abilash,



Are you able to get the Parent CI? I am also looking for the same query/way to get the parent CI. Please let me know if you have already achieve this.



I am going to look into Mark's post "Walking the ServiceNow CMDB Relationship Tree" and will update you if I figure this out.



Thanks


Kumar


Hey Kumar,



Mark's article did help me to get the parent CI, it returns all the paths to the parent CI. The challenge is when the relationship is huge i.e. if there are thousands of CI's related the parent, child, sub child etc., there causes the performance issue throwing errors resulting in the query failure...


Be careful while dealing with bigger relationships like this...



If you can further manipulate the code to stop the recursions probably this should work amazingly !!!



Regards,


Abilash