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 03:39 AM
I supposed could do
return gr.hasNext();
instead of
if(gr.next()) {
return true;
} else {
return false
}
a little more efficient in coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 03:35 AM
Is that relevant when the setLimit(1) is already defined ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 03:49 AM
gr.setLimit(1) return type is void , so you need to check it by gr.hasNext() function . And yes i agreed that gliderecord gr.setLimit(1) is more efficient then glideaggregate count function .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 03:57 AM
You can also use smaller version of script like:
var gr = new GlideRecord('a_table');
if(!gr.get('field', 'field_value')) {
gr.initialize();
gr.field_1 = field_1_value;
gr.field_2 = field_2_value;
gr.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2018 07:01 AM
Chirag, if a single field to query on, yes I would do that. I should really have added a 2nd addQuery or an addEncodedQuery to emphasise that.
It is more a question about glideRecord vs glideAggregate and whether over a period of years I have missed a trick.