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

Afrith Shariff
Tera Guru

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

Kartik Sethi
Tera Guru
Tera Guru

Hi @Mark Roethof 

 

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:
find_real_file.png

GlideAggregate performance:

find_real_file.png

 

Another approach is the script include which I can understand that you do not want to use find_real_file.png

 


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

Hi @Kartik Sethi 

 

Do you have any link/document on GlideStopWatch API?

 

Thanks

Maik Skoddow
Tera Patron
Tera Patron

Hi @Mark Roethof 

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