Gliderecord and GlideAggregate

Abhinab Achary1
Tera Guru

Hi All,

I have a query.

currently in our system we have gliderecord in script which basically counts using getrowcount and also deletes junk records .

We want to usse glideagrregate to count, so this entire functionality using glideagrregate can this be achieved?

Thanks,

Abhinab

1 ACCEPTED SOLUTION

If you are deleting records, then there is no performance gained by using GlideAggregate. They are going to do the same thing in the end. The only performance gained is using GlideAggregate's count over GlideRecord's getRowCount() for simply count operations.



My recommendation is to use GlideRecord, getRowCount() to count the records and deleteRecord() to delete them in the same server side script. If you use GlideAggregate with deleteRecord(), you're going to confuse someone somewhere.


View solution in original post

16 REPLIES 16

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Yes, see a simple example:



var count = new GlideAggregate('incident');


  count.query();   // Issue the query to the database to get all records


  while (count.next()) {


      gs.print(count.number);


  }




I get:


[0:00:00.021] Script completed in scope global: script



*** Script: INC0000001
*** Script: INC0000002
*** Script: INC0000003
*** Script: INC0000004
*** Script: INC0000005
*** Script: INC0000006
*** Script: INC0000007
*** Script: INC0000008
*** Script: INC0000009
*** Script: INC0000010
*** Script: INC0000011
*** Script: INC0000012
*** Script: INC0000013
*** Script: INC0000014
*** Script: INC0000015

The general guide line for GlideRecord().getRowCount() vs. GlideAggregate is:


  • If you're just counting records, use GlideAggregate for performance reasons. It's scalable!
  • If you need to do something ELSE to the records, like updates, deletes, etc. - Use getRowCount().


Since you are in the second case, use getRowCount(). There is not a way to do a delete operation using GlideAggregate and there's really no value in using both unless you are checking the count - e.g. If there are more than 1000 records, then delete them. For that I could see using Sergiu's idea of GlideAggregate first, then GlideRecord.


Abhinab Achary1
Tera Guru

How do i achive both count and delete records using just GlideAgreegate


You're right, as soon as we starting using Aggregate functions, we can't loop through the records anymore. That's because the query behind becomes just a SELECT COUNT() query rather than actually selecting any data.



But you can still combine both like in the below example:



var agg = new GlideAggregate('sys_user');


agg.addEncodedQuery('user_nameLIKEsnowtest100');


agg.addAggregate('COUNT');


agg.query();     // Issue the query to the database to get all records



//get the count


if (agg.next()) {    


var count = agg.getAggregate('COUNT');


}


gs.print(count);



var gr = new GlideRecord('sys_user');


gr.addEncodedQuery('user_nameLIKEsnowtest100');


gr.deleteMultiple();



I got my count displayed and also records were deleted:



[0:00:34.234] Script completed in scope global: script


*** Script: 111



The advantage of using GlideAggregate for counting purposes is that it just does a SELECT COUNT giving you directly the count rather than getting all the data back and finding the count via a loop (which is way slower).