- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2014 07:22 AM
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
Please share your Ideas....!
Solved! Go to Solution.
- Labels:
-
Multiple Versions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2015 07:10 AM
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...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2014 07:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2014 02:51 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2014 06:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2014 08:37 AM
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