Vaibhav
Mega Guru

Hi All,

As a developer, sometimes while querying data, we get confused about what should we use between GlideRecord and GlideAggregate? And is there any difference between them? Or they are the same? Or if they are different from each other then what is that which makes them different from each other?

1 Overview

  • GlideRecord 

GlideRecord is a special Java class (GlideRecord.java) that can be used in JavaScript exactly as if it was a native JavaScript class.

      • is used for database operations instead of writing SQL queries.
      • is an object that contains zero or more records from one table. Another way to say this is that a GlideRecord is an ordered list.
  • GlideAggregate

GlideAggregate is an extension of GlideRecord only. Just GlideAggregate is used to perform aggregate functions on the database and allows database aggregation (COUNT, SUM, MIN, MAX, AVG) queries to be done. This functionality can be helpful when creating customized reports or in calculations for calculated fields. The GlideAggregate class works only on number fields.

 

2 The Difference According to our ServiceNow experts

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');

 

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 into ServiceNow, which is much faster when it comes to performance.

  • This is why GlideAggregate is used.

For example: when you run a database query like select count(*) from table_name, it fetches the record count real quick, but if you run select * from table_name in your database query it will try to fetch the whole bunch of database record that would definitely take time to present the data to you.

  • Disadvantage 

The disadvantage of 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. Also, if you want to use the OrderBy function, you'll need to add the aggregate first. Example below:

var gr = new GlideAggregate('x_enig_quote_extrm_coverage');

gr.addQuery('parent', quoteSysId);

gr.addAggregate('count', 'coverage_number'); // without this line, the code throws an SQL error

gr.orderByAggregate('count', 'coverage_number');

gr.orderBy('coverage_item_number');

gr.addAggregate('sum','premium');

 

Also, as per the docs: "This can be helpful in creating customized reports or in calculations for calculated fields.

 

 

3 Related General / Platform Doc's

  1. GlideRecord
  2. GlideAggregate

 

-------

And that's it actually. Hope you like it. If any questions or remarks, let me know!

If this article helped you in any way, please then bookmark it or mark it as helpful.

 

Kind regards,
Vaibhav Chaudhari

Comments
Meenal Gharat
Giga Guru

Hello Vaibhav,

 

Can I update records using GlideAggregate count ?

Version history
Last update:
‎04-10-2020 07:42 AM
Updated by: