getRowCount() without query()

fley
Tera Contributor

Hi,

simple question here from someone just getting started with ServiceNow: 

The code example for the getRowCount() method (GlideRecord API) uses the method without first querying the table with query(). 

find_real_file.png

Elsewhere (e.g. in the ServiceNow Admin learning plan) it has query() used before:

find_real_file.png

And the example below seems to be right because if I use getRowCount() without query() I always get a count of 0 (which would make the upper coding example pointless).

Am I missing something here or is the API reference wrong?

Thanks a bunch

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

Hey,

First of all welcome to the world of SN

And I would like to share my experience with getRowCount() and there will so many things that you will be exploring regarding the same.

The first screenshot is not valid, first of all no query statement there which basically executes your database query and throw out the result, which is not happening here. Also, FYI GlideRecords are not allowed to be used at Client side scripting and only in Server side

 

Talking about 2nd screenshot, yes the code looks alright and this is how it is supposed to work with query() function after GlideRecord call.

Moreover, once you dig down further getRowCount is also not best practice and we should be using GlideAggregate for such basic mathematical like Count, Sum, Average etc

Ref link:

https://developer.servicenow.com/blog.do?p=/post/glideaggregate/

Please let me know, if it answers your query, will be more than glad to share more insights!

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

View solution in original post

3 REPLIES 3

Aman Kumar S
Kilo Patron

Hey,

First of all welcome to the world of SN

And I would like to share my experience with getRowCount() and there will so many things that you will be exploring regarding the same.

The first screenshot is not valid, first of all no query statement there which basically executes your database query and throw out the result, which is not happening here. Also, FYI GlideRecords are not allowed to be used at Client side scripting and only in Server side

 

Talking about 2nd screenshot, yes the code looks alright and this is how it is supposed to work with query() function after GlideRecord call.

Moreover, once you dig down further getRowCount is also not best practice and we should be using GlideAggregate for such basic mathematical like Count, Sum, Average etc

Ref link:

https://developer.servicenow.com/blog.do?p=/post/glideaggregate/

Please let me know, if it answers your query, will be more than glad to share more insights!

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

fley
Tera Contributor

Thanks for the fast response. Yeah, the documentation says that GlideAggregate is the way to go in a production environment.

I was just confused because the upper screenshot is straight from the official API reference. Thanks for clearing this up!

 

 

Always!

Feel free to reach out and also share new things that you explore!

Best Regards
Aman Kumar