Use GlideAggregate Function to identify - MIN,MAX and COUNT

ersureshbe
Giga Sage
Giga Sage

Hi Team,

I'm trying to writing script to find Min and Max value using below script. The below script showing the output as

Minimum Value: 1
Maximum Value: 1

When I'm using while loop instead If getting below output.

Minimum Value: 1
Maximum Value: 1
Minimum Value: 2
Maximum Value: 2
Minimum Value: 3
Maximum Value: 3
Minimum Value: 4
Maximum Value: 4
Minimum Value: 5
Maximum Value: 5

 

find_real_file.png

var gr = new GlideAggregate('Table name');
gr.addQuery('u_item_name','u_activity');
gr.addAggregate('MIN','u_activity_number');

gr.addAggregate('MAX','u_activity_number');
gr.query();

if(gr.next()) //while(gr.next())
{
var Min, Max;
Min = gr.getAggregate('MIN','u_activity_number');
Max = gr.getAggregate('MAX','u_activity_number');
gs.log('Minimum Value:'+Min);
gs.log('Maximum Value:'+Max);
}

 

Regards,

Suresh Loganathan.

Regards,
Suresh.
3 REPLIES 3

Kunal Varkhede
Tera Guru
Hi sureshloganathan,

Refer this code.

var
count = new GlideAggregate('incident'); count.addAggregate('MIN','sys_mod_count'); count.addAggregate('MAX','sys_mod_count'); count.addAggregate('AVG','sys_mod_count'); count.groupBy('category'); count.query(); while(count.next()){ var min = count.getAggregate('MIN','sys_mod_count'); var max = count.getAggregate('MAX','sys_mod_count'); var avg = count.getAggregate('AVG','sys_mod_count'); var category = count.category.getDisplayValue(); gs.log(category +" Update counts: MIN = "+ min +" MAX = "+ max +" AVG = "+ avg);}

Go through the GlideAggregate Docs
https://docs.servicenow.com/bundle/jakarta-application-development/page/script/glide-server-apis/con...


Please Mark Correct/Helpful answer if it helps you.

Thanks and Regards,
Kunal.

Kunal,

Thanks for your update. It was referred before my post. There is no change or i cant identify what i wrote and what you mentioned above. 

Regards,

Suresh Loganathan.

Regards,
Suresh.

ersureshbe
Giga Sage
Giga Sage

Found the error in above script.

The above query, I missed the gr.setGroup(false). If i add aggregate function is working as expected.

R,

Suresh Loganathan

Regards,
Suresh.