Consciously using things the wrong way in order to get funny results is a passion for many people. It’s this spirit that leads to true innovation and drives humanity forward.
So, GlideAggregate. The odd but useful cousin of GlideRecord. Unlike other stuff, this one is actually quite well documented. Its main purpose is to count things. Nothing more, nothing less. This is a major oversimplification, as it can count, sum, average, and a thousand other things, but it always boils down to one – counting things. It is very quick since it works mainly on the database level. Unlike GlideRecord, which drags full-size records around, GlideAggregate travels light, taking only the most necessary stuff. We won’t be looking at its actual intended use in this article, as it has detailed documentation for that. So, take a look at it for some information.
Looking at the almost identical base syntax of both methods and knowing one has a significant speed advantage, it’s only logical for some funny thoughts to start going around one’s mind. Can’t I just use the fast one instead? Then I can get into all my scripts and just replace the “Record” part with “Aggregate”, and everything will run so insanely fast that everyone will be amazed, and they’ll make me a king of all platform developers, and I’ll fight princesses and save dragons and… or was that the other way around?
Never mind, the short answer is NO. As with everything else in our part of the multiverse, there are some limits and tradeoffs too. GlideAggregate looks a lot like GlideRecord, but it cannot do the same things. For example, you’ll have a hard time updating any records that you pulled using GA. There’s also not much point in deleting, as we already know the fastest option there is to use .deleteMultiple() with GR. Retrieving, though, is where it can blow you away. Again, not in all cases, but here’s a sample one:
The good old dev instance is loaded with ~35000 fake incidents. We’ll go through them using both methods and try to get some measurable results.

var incident = new GlideRecord('incident');
incident.query();
while(incident.next()) {
gs.info(incident.number);
}
We’re getting them all with a GlideRecord. I just want to get their numbers and print them. Here’s the result:

Bear in mind that these are almost identical, nearly empty records that I’ve mass-inserted just for the test, so the database runs some smart automagical witchcraft to make things super-fast, hence the 35 seconds needed to process 35000 records. In a real environment, think ten times that result. Let’s try the GA now:
var incident = new GlideAggregate('incident');
incident.query();
while(incident.next()) {
gs.info(incident.number);
}

There, you see? I told you it was faster! OK, thanks for reading, that’s all, you go on and have an awesome day now.
I’m only making sure you’re paying attention. Here’s the real magic – we need to group it:
var incident = new GlideAggregate('incident');
incident.groupBy('number');
incident.query();
while(incident.next()) {
gs.info(incident.number);
}

Not bad, right? And there’s more – grouping only shows unique values, so when used in the right situation, this method can save you time twice – once while retrieving and a second time by sparing you the need to remove duplicates. This, of course, can be a foul surprise if you don’t actually need to remove duplicate values, so be careful and act accordingly.
But if they are equally fast with baseline code, and grouping makes all the difference, then maybe grouping is the secret sauce? Let’s check:
var incident = new GlideRecord('incident');
incident.groupBy('number');
incident.query();
while(incident.next()) {
gs.info(incident.number);
}

Pretty much the same result, with the difference within the margin of error. So, there you have it. Don’t be afraid to break functions in a controlled manner or use them in some funny new way.
Subscribe to our newsletter to receive our latest tips and insights about ServiceNow and more.