Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

getRowCount returning the incorrect number of rows

brinley_powell
Tera Expert

Hi ServiceNow Community,

 

I have an issue with the code below when running in a background script.

 

var child_so_list = ['61faa1851bf6c8d0a7c2b8c2cd4bcbe0','1dfaa1851bf6c8d0a7c2b8c2cd4bcb1d','0dfaa1851bf6c8d0a7c2b8c2cd4bcb1a'... 342 sys id in the list ];

var company_sys_id = '4bbf4192db2beb8088065487dc96199b';
var gr_so = new GlideRecord('service_offering');
gr_so.addQuery('company', '=', company_sys_id);
gr_so.addQuery('sys_id' ,'IN' , child_so_list.toString());
gr_so.query();

gs.print(gr_so.getRowCount());

 

There is only one service offering in the array that has company as the one queried. And so it should only return 1 but the query is returning 342 (the number of sys id's in the array). I then ran this second script to try and see if it was to do with the number of sys id's in the array.

 

var child_so_list = ['61faa1851bf6c8d0a7c2b8c2cd4bcbe0','1dfaa1851bf6c8d0a7c2b8c2cd4bcb1d','0dfaa1851bf6c8d0a7c2b8c2cd4bcb1a','b0fa61851bf6c8d0a7c2b8c2cd4bcb4e'...  342 sys id in the list];

var so_count = child_so_list.length

for(var i = 0;i<so_count;i++){
var company_sys_id = '4bbf4192db2beb8088065487dc96199b';
var gr_so = new GlideRecord('service_offering');
gr_so.addQuery('company', '=', company_sys_id);
gr_so.addQuery('sys_id' ,'IN' , child_so_list.toString());
gr_so.query();

gs.print(gr_so.getRowCount());
child_so_list.pop();
}

 

And with this script when the array gets down to 100 items it starts to correctly return the row count. Is there a limit on the number of items you can have in an array for getRowCount to work. After speaking with a colleague they suggested I use the below line instead of the getQuery lines.

 

gr_so.addQuery('company', '4bbf4192db2beb8088065487dc96199b').addCondition('sys_id', 'IN', child_so_list.toString());

 

And this works regardless of how many items are in the array.

 

Thanks in advance!

1 ACCEPTED SOLUTION

brinley_powell
Tera Expert

This is a known bug in ServiceNow - PRB1638779- getRowCount() on GlideRecord is returning incorrect value.

 

They provided the workaround of - If you dont need to iterate through the results and just wants to get a count, you can use

GlideAggregate getAggregate()

View solution in original post

1 REPLY 1

brinley_powell
Tera Expert

This is a known bug in ServiceNow - PRB1638779- getRowCount() on GlideRecord is returning incorrect value.

 

They provided the workaround of - If you dont need to iterate through the results and just wants to get a count, you can use

GlideAggregate getAggregate()