What is the difference between GlideAggregrate vs GetRowCount() in servicenow?

Shilpi Sharma2
Tera Contributor

Hi All,

Please tell me the difference between GlideAggregrate vs GetRowCount() in servicenow

 

5 REPLIES 5

Shrutika Surwad
Kilo Guru

GlideRecord: 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.

example:
gets a count of the number of records in a table:

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

 

GetRowCount(): Retrieves the number of rows in the query result.

Example
var gr = new GlideRecord('incident')
gr.query();
gs.info("Records in incident table: " + gr.getRowCount());

 

Thanks,

Shrutika Surwade

find_real_file.png

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

GlideAggregate - getRowCount()

Retrieves the number of rows in the GlideAggregate object. Refer to https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_ScopedGlideAggregateGetRowCount

GlideRecord - getRowCount()

Retrieves the number of rows in the query result.. Refer to https://developer.servicenow.com/app.do#!/api_doc?v=london&id=r_ScopedGlideRecordGetRowCount

In the links above, you will find some example.

I would also suggest to read the solution provided in the thread below, it will clarify your doubt:

https://community.servicenow.com/community?id=community_question&sys_id=8ff29b80db6c2f002be0a851ca96...

If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thank you

Cheers
Alberto

Vishal Khandve
Kilo Sage

GlideAggregate enables you to easily create database aggregation queries.

The scoped GlideAggregate class is an extension of GlideRecord and provides database aggregation (COUNT, SUM, MIN, MAX, AVG) queries. This functionality can be helpful when creating customized reports or in calculations for calculated fields. The GlideAggregate class works only on number fields.

When you use GlideAggregate on currency or price fields, you are working with the reference currency value. Be sure to convert the aggregate values to the user's session currency for display. Because the conversion rate between the currency or price value (displayed value) and its reference currency value (aggregation value) might change, the result may not be what the user expects.

 

example-

ar count = new GlideAggregate('incident');
  count.addAggregate('MIN', 'sys_mod_count');
  count.addAggregate('MAX', 'sys_mod_count');
  count.addAggregate('AVG', 'sys_mod_count');
  count.groupBy('category');
  count.query();
  gs.info(count.getRowCount());
  while (count.next()) {  
     var min = count.getAggregate('MIN', 'sys_mod_count');
     var max = count.getAggregate('MAX', 'sys_mod_count');
     var avg = count.getAggregate('AVG', 'sys_mod_count');
     var category = count.category.getDisplayValue();
     gs.info(category + " Update counts: MIN = " + min + " MAX = " + max + " AVG = " + avg);
  }

Jaspal Singh
Mega Patron
Mega Patron

Hi Shilpi,

 

  • GlideAggregate - If you're just counting records.
  • getRowCount() - If you need to do something ELSE to the records, like updates, deletes, etc.

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.