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

Hi Ahinab,



  1. var count = new GlideAggregate('incident');  
  2. count.query();   // Issue the query to the database to get all records  
  3. while (count.next()) {    
  4. count.deleteRecord();
  5.       gs.print(count.number);  
  6.   }  
  7. gs.print(count.getRowCount());

You can use both getRowCount() and deleteRecord() in GlideAggregate.


While deleteRecord() is not documented, I know that GlideAggregate shares many of the same methods (like addQuery()) as GlideRecord. Your solution is basically the same as GlideRecord, so there's not counting going on in your example.


I just did some testing and have a strong suspicion that GlideAggregate is an extension of GlideRecord. The query setup is the same and even methods like getRowCount() and deleteRecord() are available to GlideAggregate. Although, I don't recommend using them that way as there is no direct benefit.



Can you describe the problem you are trying to solve? That would point us in the direction of a proper technical solution.


My requirement is that in our audit /Servicenow inspection , Servicneow provided the update that performance related to change certain codes. the getRowcount to count records used in scripts to GlideAggregate.



But there is a place , you can say multiple script (CS , BR, SInclude) where getRowcount is used) So ia m not sure if i should use it as , using GlideAgrregate class I am able to get count but not delete the records , in that case i need to use Sergiu's solution.




But considering both scnario which is better , USe getRowCount as is , or create two objects as Sergiu suggested.snowone.png


If you do it once, then yes, getRowCount() is much easier.



But if would have needed to do many times it in a loop, then getRowCount is about 20 times slower than GlideAggregate, so for large amount of records it can cause significant delay, see below example:




var gr = new GlideRecord('sys_metadata');


gr.query();


gs.print(gr.getRowCount());



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


*** Script: 243635




var gr = new GlideAggregate('sys_metadata');


gr.addAggregate('COUNT');


gr.query();



if(gr.next())


  gs.print(gr.getAggregate('COUNT'));



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


*** Script: 243635




700ms on getRowCount vs 35ms on GlideAggregate