Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

what does groupBy() in glideAggregate exactly do?

Varun Sharma
Tera Expert

Hi Experts,

 

i was trying to clean up duplicate CI, and i found that my code works if i group By all three category (parent, child & type) and not if i just groupBy one category.  

May i ask what groupBy(), does here exactly? why is this code is not working without these groupings

var agg = new GlideAggregate('cmdb_rel_ci');
agg.addQuery(encodedQuery);
agg.setLimit(batch);

agg.groupBy('parent');
agg.groupBy('type');
agg.groupBy('child');

agg.addAggregate('COUNT');
agg.addHaving('COUNT', '>', '1'); // if Query finds more than 1 record with same parent, type and child
agg.query();

is the groupBy here is same as the screenshot in list view? 

if so how can we have group by different category ? 

find_real_file.png

I'm still a noob and trying to understand the platform. 

I'll be grateful if you can explain that.

 

Regards, 

Varun Sharma

1 ACCEPTED SOLUTION

Hi @Varun Sharma 

 

No, It will take all.

Example:

In my PDI, I have total 558 CIs.

agg.groupBy('parent'); // answer is 180
agg.groupBy('type'); // answer is 19
agg.groupBy('child'); // answer is 189

 

If I grouped all three fields.

Then i'm getting 558.

Here what happening is, If you have atleast one Parent or type or child. It will count.

 

If I grouped only one field(parent).

Then I'm getting 180.

Here what happening is, It will take only parent field which are not empty.

 

If my response is helpful, then Please mark as Correct Answer/Helpful.

Please check and let us know.

Thanks:)

Thanks,
Shakeel Shaik 🙂

View solution in original post

10 REPLIES 10

ShivaniP6387693
Mega Contributor

i need answer

to my question why we use GroupBy in serviceNow and Use ?