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.

How to count Distinct in GlideRecord?

sarahm
Tera Contributor

Hi Team,

I need to count the distinct number of requested approvals in the sysapproval_approver table.

Like as shown in the below screenshot, the total count of approvals in requested state is 60, but after GroupBy of the Approval For the result is 31. I am trying to achieve this by glideRecord but it's not working. Can you please assist.

                              var count = 0;

                               var gr = new GlideAggregate(‘sysapproval_approver’);

                               gr.addQuery (‘state’,’requested’);

                               gr.addAggregate('COUNT','sysapproval');

                               gr.orderByAggregate('COUNT','sysapproval');

                               gr.query();

                              if(gr.next())

                                count = gr.getAggregate('COUNT','sysapproval');

                               gs.print(parseInt(count));

 

find_real_file.png 

Thanks in advance,

Sarah

1 ACCEPTED SOLUTION

Brian Lancaster
Kilo Patron

If you want the total number to be like when they are group by try using GlideRecord instead of GlideAggregate.

var count = 0;
var counted = [];
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery ('state','requested');
gr.query();
while(gr.next()) {
	if (!counted[gr.sysapproval]){
		counted[gr.sysapproval] = true;
		count++;
		
	}
}
gs.print(count);

View solution in original post

10 REPLIES 10

This works Brian! Thanks a lot.

Regards,

Sarah