The CreatorCon Call for Content is officially open! Get started here.

table.hasRecords()

Mark Roethof
Tera Patron
Tera Patron

Hi all,

Don't mind the title, that doesn't exist, though that is what I am after! Just playing around, and want to pass into a scripted condition field (which should evaluate to true or false) if:

A table has 1 or more records.

Currently I'm doing this with:

var gr = new GlideRecord('incident_task');gr.setLimit(1);gr.query();gr.hasNext();

Works fine. Though, just wondering is there a nicer method to achieve the same?!

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

5 REPLIES 5

Markus Kraus
Kilo Sage

Fastest and shortest solution i can think of is GlideQuery

new GlideQuery('incident').selectOne().isPresent()

 

EDIT: Did some testing, the results are: GlideRecord > GlideAggregate >> GlideQuery.
GlideRecord beats GlideAggregate most of the times, both beat GlideQuery by a factor of over 2.

EDIT2: The performance table is as follows (after more testing on several customer [dev] instances):
GlideRecord: 100%
GlideAggregate: 104%
GlideQuery: 260%

Example result:

GlideRecord: 0.849
GlideAggregate: 0.926
GlideQuery: 2.322



Here's the test script (global scope):

for (var j = 0; j < 5; j++) {
var numTest = 1000;
var totalTime;

totalTime = 0;
for (var i = 0; i < numTest; i++) {
 var test = new GlideStopWatch()
 var gr = new GlideRecord('incident');gr.setLimit(1);gr.query();gr.hasNext();
 totalTime += test.getTime();
}
gs.print('GlideRecord: ' + (totalTime / numTest));

totalTime = 0;
for (var i = 0; i < numTest; i++) {
 var test = new GlideStopWatch()
 var ga = new GlideAggregate('incident');ga.addAggregate('COUNT');ga.query();ga.getAggregate('COUNT') > 0;
 totalTime += test.getTime();
}
gs.print('GlideAggregate: ' + (totalTime / numTest));

totalTime = 0;
for (var i = 0; i < numTest; i++) { 
 var test = new GlideStopWatch()
 new GlideQuery('incident').selectOne().isPresent();
 totalTime += test.getTime();
};
gs.print('GlideQuery: ' + (totalTime / numTest));
gs.print("###");
}