GlideRecord, GlideAggregate and when to choose one over the other

πŸ’‘
Specific record operations? GlideRecord
Aggregated data and calculations? GlideAggregate

If you're anything like me you must have spent a fairbit of time scratching your head over when to use GlideRecord and when to switch to GlideAggregate. I want to dive into the nitty-gritty of these concepts especially when need of using GlideRecord's getRowCount().

GlideRecord

What is it? πŸ”

GlideRecord is a ServiceNow API which can be used for all operations on individual database records in ServiceNow, be it querying, updating, or deleting.

Pros 🌞

  • Detailed Access: Perfect for diving deep into records
  • Versatility: It can be used beyond querying and can be used for inserts, updates and deletions.
  • Beginer-friendly: Easy to understand or self explaining methods

Cons πŸ˜…

  • Performance: It will load each record from the query, so performance can for tables with huge number of records.
  • Below is a sample script to get the count of all Critical incidents.
  • In this case, GlideRecord first loads all the Critical incidents and then count the result using the getRowCount method
var gr = new GlideRecord('incident');
gr.addQuery('priority', 'Critical');
gr.query();
gs.print('Total Critical Incidents: ' + gr.getRowCount());
  • Memory consumption: Expect a significant memory use for begger data sets

GlideAggregate

What is it? πŸ”

GlideAggregate is a ServiceNow API specialized for calculations across records. As the name suggests you can aggregate the records and perform opeations like count, sum, average.

Pros 🌞

  • Performance: Build for efficiency and quickly retrieves aggregated data without loading each record
var ga = new GlideAggregate('incident');
ga.addQuery('priority', 'Critical');
ga.addAggregate('COUNT');
ga.query();
if(ga.next()) {
    gs.print('Total Critical Incidents: ' + ga.getAggregate('COUNT'));
}
  • Aggregation Mastery: From averages to min/max calculations, it's your aggregation Swiss Knife
  • Memory-efficiency: Without loading individual records, it's easier on your system's memory.

Cons πŸ˜…

  • Strictly Aggregates: Powerful at aggregations but doesn't support operations like GlideRecord
  • User Friendliness: These methods might intimidate for users new to scripting

Why to chose GlideAggregate over GlideRecord's getRowCount() πŸ€”


While 'getRowCount()' with GlideRecord does the job in getting the count of the result, there is a compelling reason to go for GlideAggregate.

Performance

GlideAggregate usually fetches counts faster. With 'getRowCount()', GlideRecord loads all records just to count them. GlideAggregate, on the otherhand, directly retrieves this count ensuring efficiency.

Until next time, happy coding! πŸ’»πŸš€

Subscribe to karteek

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe