GlideAggregate MAX
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 02:50 PM
I have imported a number of records from Oracle into ServiceNow. Have an Integer field called student_id that the records loaded just fine. The last student_id loaded is #90358. When I run the following GlideAggregate:
var gaStudent = new GlideAggregate('student');
gaStudent.addAggregate('MAX', 'student_id');
gaStudent.query();
if (gaStudent.next()) {
gs.info('Last Student ID = ' + gaStudent.getAggregate('MAX', 'student_id'));
}
The last student_id printed in the log is 1.
I then loaded all the student ids into an array and grabbed the MAX value in the array:
var gr = new GlideRecord('student');
gr.orderBy('student_id');
gr.query();
var log = [];
while (gr.next()) {
log.push(gr.student_id);
}
var max_of_array = Math.max.apply(Math, log);
gs.info('Last Id: {0}', max_of_array);
This prints the correct value but takes a bit longer to process.
Any issues with my query?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-04-2023 09:05 PM
HI @Stanley Martin ,
I trust you are doing great.
the direct casting in GlideAggregate might not be possible, a workaround is to use an encoded query to filter out non-numeric values before running the aggregate. This way, you ensure that only numeric values are considered for the aggregation.
var gaStudent = new GlideAggregate('student');
gaStudent.addEncodedQuery('student_idISNOTEMPTY^student_id>=0'); // This ensures only numeric values are considered
gaStudent.addAggregate('MAX', 'student_id');
gaStudent.query();
if (gaStudent.next()) {
gs.info('Last Student ID = ' + gaStudent.getAggregate('MAX', 'student_id'));
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-05-2023 06:25 AM
Same issue. Keeps coming back with 1 as the last student id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 01:54 AM
By default, GlideAggregate groups results. You should explicitly disable grouping using setGroup(false), like this:
var gaStudent = new GlideAggregate('student');
gaStudent.addAggregate('MAX', 'student_id');
gaStudent.setGroup(false); // Important: disables grouping
gaStudent.query();
if (gaStudent.next()) {
gs.info('Last Student ID = ' + gaStudent.getAggregate('MAX', 'student_id'));
}
On a side note, for your case, you can just use GlideRecord with orderByDesc and setLimit(1) to achieve the same result more directly:
var gr = new GlideRecord('student');
gr.orderByDesc('student_id');
gr.setLimit(1);
gr.query();
if (gr.next()) {
gs.info('Last Student ID = ' + gr.student_id);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2025 02:10 AM
HI @Stanley Martin ,
I hope you are good , the thing is glide aggregate by default groups the results, so you should add one thing
setGroup(false);
this will resolve your error
here is the corrected version below try it and let me know if it works or not
var gaStudent = new GlideAggregate('student'); gaStudent.addAggregate('MAX', 'student_id'); gaStudent.setGroup(false); // Important: disables grouping gaStudent.query(); if (gaStudent.next()) { gs.info('Last Student ID = ' + gaStudent.getAggregate('MAX', 'student_id')); }