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 06:36 AM
Try writing a script include and call the function in the business rule, writing a function that checks if the record is available and calling the function in business rule will actually save your time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 06:47 AM
Hi
Even a question from you is mind-boggling!!
I could not think of any better option than what you have already in place. I would just recommend using GlideAggregate instead of GlideRecord.
When I tried to check the performance (already we know that GlideAggregate is better):
- GlideRecord's performance (even with setLimit😞 1millisecond~12milliseonds
- GlideAggregate performance: was a bit consistent which ranged from 2 millisecond~5millisecond.
Code used to check performance:
var test = new GlideStopWatch();
//var gr = new GlideRecord('incident');gr.setLimit(1);gr.query();gr.hasNext();
var ga = new GlideAggregate('incident');ga.addAggregate('COUNT');ga.query();ga.getAggregate('COUNT') > 0;
gs.print('Time taken to execute: ' + test.getTime());
GlideRecord performance:
GlideAggregate performance:
Another approach is the script include which I can understand that you do not want to use
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2022 09:50 AM
Hi
Do you have any link/document on GlideStopWatch API?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2022 05:57 PM
Hi
I don't know whether ServiceNow has such a Utils method in place, but even if it were, there is no guarantee that such a helper method would be fast.
In the following blog post the different approaches are examined regarding the execution speed and it seems, that your approach is the fastest one.
Kind regards
Maik