- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 02:25 AM
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 05:51 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 02:59 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 04:36 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 04:08 AM
How do i achive both count and delete records using just GlideAgreegate
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2018 04:32 AM
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).