How to count number of child records - Relationship table

Paul125
Kilo Guru

I am trying to find out the number of childs that exist to a parent by gliding the relationship table, I am using the below script but result coming as 'undefined'.

var ciSysIds = ['09653fa7db4f9b00e0a45bc0cf96192c'];
var agg1 = new GlideRecord('cmdb_rel_ci');
agg1.addAggregate('count');
agg1.addQuery('parent','IN',ciSysIds);
agg1.addQuery('type.parent_descriptor','members');
agg1.addQuery('type.child_descriptor','member of');
agg1.groupBy('child');
agg1.query();
if(agg1.next()) {
var count = agg1.getAggregate('count');
gs.info(count);
1 ACCEPTED SOLUTION

Paul125
Kilo Guru
parentSysIds = agg.getValue('parent');
var agg1 = new GlideAggregate('cmdb_rel_ci');
agg1.addAggregate('count');
agg1.addQuery('parent','IN',parentSysIds);
agg1.addQuery('type.parent_descriptor','members');
agg1.addQuery('type.child_descriptor','member of');
agg1.groupBy('parent');
agg1.query();
if(agg1.next()) {
var count = agg1.getAggregate('count');
gs.info(count);
}

View solution in original post

8 REPLIES 8

Thanks for the reply Pradeep. You suggestion was working but I am only seeing count as 1 when there are 5 childs to the CI that I am gliding for. Any thoughts?

Thanks for the update, Ryan. Replace if with while loop in your script and check once.

It's working after changing IF to While but I am seeing individual counts instead of number. Thanks!

*** Script: 1
*** Script: 1
*** Script: 1
*** Script: 1
*** Script: 1

Paul125
Kilo Guru
parentSysIds = agg.getValue('parent');
var agg1 = new GlideAggregate('cmdb_rel_ci');
agg1.addAggregate('count');
agg1.addQuery('parent','IN',parentSysIds);
agg1.addQuery('type.parent_descriptor','members');
agg1.addQuery('type.child_descriptor','member of');
agg1.groupBy('parent');
agg1.query();
if(agg1.next()) {
var count = agg1.getAggregate('count');
gs.info(count);
}