Record Exists - GlideAggregate or GlideRecord
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 02:00 AM
Looking at some existing Business rules for my client and they are checking to see if a record exists before inserting - fair enough. The thing I find odd is that they are using GlideAggregate whereas I would use GlideRecord
Am I missing a trick here ?
var vCount;
var gr = new GlideAggregate('a_table');
gr.addQuery('field',.value);
gr.addAggregate('COUNT');
gr.query();
gr.next();
vCount = gr.getAggregate('COUNT');
if (vCount > 0){
return(true); // Entry already exists
}
return(false);
new GlideRecord('a_table');
gr.addQuery('field','value');
gr.setLimit(1);
gr.query();
if(gr.next()) {
return true
}
return false;
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 02:22 AM
Hi,
According to wiki, "The GlideAggregate class is an extension of GlideRecord and 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."
Note: Using GlideRecord to count rows can cause scalability issues as tables grow over time, because it retrieves every record with the query and then counts them. GlideAggregate gets its result from built-in database functionality, which is much quicker and doesn't suffer from the scalability issues that GlideRecord does.
So it is better to use GlideAggregate instead of GlideRecord in such scenarios.
Mark Correct or helpful if it is so.
Regards,
Chandra Prakash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 03:35 AM
But the GlideRecord query is setting the limit to 1 record, whereas glideAggregate will be reading all records to do the count - which is not efficient - hence my question

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 02:27 AM
use gr.hasNext() instead of gr.next()
new GlideRecord('a_table');
gr.addQuery('field','value');
gr.setLimit(1);
gr.query();
if(gr.hasNext()) {
return true
}
return false;

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 02:38 AM
is it helpful for you ?????