getRowCount() incorrect when using GlideRecordSecure()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2020 05:02 AM
Hi,
When we have some row level security ACLs on a table we can use GlideRecordSecure() to iterate over the rows on which the user has permissions without having to explicitly put if statement to check if gr.canRead() for every gr.next() in the loop. This is great but if we do a gr.getRowCount() it returns the count without subtracting the records which the user is not allowed to see. Here is one example for better understanding:
var table = "someTable";
/*lets say this table has 6 records. 4 records with field u_active=true
and 2 records with field u_active=false */
/*create row level ACL which based on the user role will restrict this role
to access only the records where u_active=true(in total this role should
have access to 4 records)*/
var grs = new GlideRecordSecure(table);
grs.query();
console.log('GRS: ' + grs.getRowCount()); /* this will return 6 (which is wrong
because the user doesn't have permissions to 2 of the records) */
var scnt = 0; //create manual counter to increase on grs.next()
while(grs.next()){
scnt++;
}
console.log('GRS Manual Count: ' + scnt); /* this will return 4 which is correct
because the iteration skips the records to which we don't have permissions */
//compare with GlideRecord
var gr = new GlideRecord(table); //this will return 6
gr.query();
console.log('GR: ' + gr.getRowCount());
var gcnt = 0;
while(gr.next()){
gcnt++;
}
console.log('GR Manual Count: ' + gcnt); //this will return 6
So the question is how to get the correct row count when using the GlideRecordSecure. Iterating and calculating it manually like in the example above is not an option because we might have large data set and use pagination.
I was thinking also to use GlideAggregate somehow but couldn't think of a way.
Any help is appreciated. 🙂
- Labels:
-
Scripting and Coding
- 3,362 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2020 01:12 AM
As stated earlier see:
https://docs.servicenow.com/bundle/orlando-application-development/page/script/glide-server-apis/top...
As you can see, GlideRecordSecure does not filter away records that the given user cannot read.
It just have a built-in "if(!gr.canRead()) continue;" to skip records automatically instead of you needing to do it manually.
In normal GlideRecords you need to do this manually.
RowCount is still identical to GlideRecord and just count the result of the response of the Database query.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2020 02:12 AM
Hi,
Thanks for the information. This explains why the getRowCount behaves like this for GlideRecordSecure().
I hope someone comes up with a solution on how to get the proper total count which is very useful if you have pagination and want to display the total count of the rows.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2020 06:45 AM
I would suggest to make your own count
Something like
var counter = 0;
while(GlideRecordSecure.next()){
counter++;
}
End end result will be the correct amount.
Glideaggregate doesnt with correctly as well so you need to manually count the amount of readable records by the given user

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2020 09:23 AM
Hi there,
Thanks for your hint but I already gave this example in the original post and explained that this approach is not useful when using big data set with pagination since you are getting only chunks of data at a time so you can not count all rows manually.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2020 09:51 PM
Hi Nikolay
Im pretty sure that its still possible to count the amount of records recieved even during pagination. Its just a matter of setup.
Depending on approach this is an example
(function mainMethod(){
var counter = 0;
while(counter < 100){
counter += this.countFromAnotherMethod();
gs.print(counter);
}
})();
function countFromAnotherMethod(){
//Do something and return amonut of records from pagination
return Math.floor(Math.random() * 10);
}
You can also turn it around (while counter > 0) and instead of add to counter you can set it to amount of records from pagination because sooner or later the amount of records from pagination will be 0