Use GlideAggregate Function to identify - MIN,MAX and COUNT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-25-2019 01:14 AM
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
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.
Suresh.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-25-2019 01:28 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-25-2019 02:22 AM
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.
Suresh.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2019 06:02 AM
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
Suresh.