Difference between GlideRecord() and GlideAggregate()


As a newer developer, I tend to forget basic JavaScript methods and I find myself doing lots of Google searching. In ServiceNow, there are plenty of methods you should know to become a fluent developer in the platform. GlideRecord and GlideAggregate are no exception.

Let's say you wanted to query all active incidents and then disable them by setting their active field to false. Easy, right? Well this is where GlideRecord comes handy:
GlideRecord
var rec = new GlideRecord('incident') ;
rec.addQuery('active',true);
rec.query(); 
while(rec.next()) { 
  rec.active = false;
  gs.print('Active incident ' + rec.number = ' closed');
  rec.update(); }

But what if we wanted to just simply retrieve the number of records in a table? This is where we can use GlideAggregate:

GlideAggregate
var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if(count.next()) 
   incidents = count.getAggregate('COUNT');

The Difference


I'm sure there are obvious differentiators by now, but what is really the difference between the two? Can't you just use GlideRecord to retrieve the sum of incident records? Short answer: No.

There are alternatives to using GlideAggregate, but the options are slim to none. You can either use the getRowCount() method from GlideRecord, or just use GlideAggregate. Using GlideRecord to count rows of records will cause scalability issues as tables can grow over time. This method retrieves every record and counts them. GlideAggregate retrieves the data from the actual MySQL database that's built in to ServiceNow, which is much faster when it comes to performance.

The GlideAggregate class is an extension of GlideRecord and allows database aggregation (COUNT, SUM, MIN, MAX, AVG) queries to be done. This can be helpful in creating customized reports or in calculations for calculated fields. read more

The disadvantage to using GlideAggregate, is that you are unable to access details in a specific record. In this case, you would use GlideRecord() to manipulate/read the fields on any given record.

Avatar
Maverick Embry
ServiceNow Developer / Admin

Web development && ServiceNow