table.hasRecords()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 05:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 10:03 PM
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("###");
}