Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Record Exists - GlideAggregate or GlideRecord

poyntzj
Kilo Sage

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;

9 REPLIES 9

I supposed could do

return gr.hasNext();

 

instead of

if(gr.next()) {

return true;

} else {

return false

}

 

a little more efficient in coding

Is that relevant when the setLimit(1) is already defined ?

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 .

chirag_bagdai
ServiceNow Employee

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();
}

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.