What is the best way to replace getRowCount in scripts for simple record counts?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 06:10 PM
I've seen mention of recommendations/best practices to not use getRowCount in scripts, and instead use GlideAggregate and ('COUNT') to quickly get a count to improve script performance. I'm not sure if that's really applicable in all cases and would like to know how others might handle something like:
if (itemsr.getRowCount() > 1) {
I don't know if there's much, if any performance to be gained if we're just trying to find out if the count is > 1, but I'm not 100% sure. Below is the rest of the script block for reference. Any recommendations are greatly appreciated!
getActiveCatalogItems: function () {
var items = [];
var catItem = new GlideRecord('sc_cat_item');
catItem.addEncodedQuery("active=true");
catItem.orderBy(name);
catItem.query();
while (catItem.next()) {
var itemsr = new GlideRecord("item_option_new");
itemsr.addQuery('cat_item', catItem.sys_id);
itemsr.addQuery('type', 'NOT IN', '20,24,19');
itemsr.query();
if (itemsr.next()) {
if (itemsr.getRowCount() > 1) {
var obj = {};
obj.name = catItem.name.toString();
obj.sys_id = catItem.sys_id.toString();
items.push(obj);
}
}
}
return JSON.stringify(items);
},
- Labels:
-
Scripting and Coding
- 2,883 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 07:10 PM
Hi,
The best practice guidance is within the context that if you're solely just trying to get a row count, use GlideAggregate. If you're going to actually use other components of the GlideRecord API, and you're working with the records returned anyway, then you can use getRowCount in conjunction with the other methods of the API and there's really no performance difference.
That said, you can also tighten up this line by changing it to:
if (itemsr.next() && itemsr.getRowCount() > 1) {
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2022 06:42 AM
Hi
Thanks for marking my reply as Helpful.
If it ends up guiding you Correctly, please also mark it as Correct.
Thanks and take care! 🙂
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-07-2022 04:36 AM
Hi
Thanks for marking my reply as Helpful.
If it ends up guiding you Correctly, please also mark it as Correct.
Thanks and take care! 🙂
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-25-2022 07:18 PM
Hi Marcel,
If the record is not going to be accessed, I'll just use hasNext().
if (itemsr.next() && itemsr.hasNext()) {