GlideRecordSecure is a class inherited from GlideRecord that performs the same functions as GlideRecord, and also enforces ACLs.
Thanks,
Dhananjay.
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. 🙂
06-18-2020 10:41 PM
Hello,
I think the below link will be helpful to you..
Specially the section where they have used GlideRecordSecure with row count effectively , see example of task_ci
Mark my ANSWER as CORRECT and HELPFUL if it works
06-19-2020 12:41 AM
Hi,
Thanks for the info, but this is the official documentation for Orlando which is almost the same as for New York and the example given will lead to the same results as explained above.
06-18-2020 10:50 PM
If you need to count rows, you have two options: the getRowCount() method from GlideRecord, or GlideAggregate. Using GlideRecord to count rows can cause scalability issues as tables grow over time, because it retrieves every record with the query and then counts them. GlideAggregate gets its result from built-in database functionality, which is much quicker and doesn’t suffer from the scalability issues that GlideRecord does.
For more details refer this,
Read this for GlideAggregate concept
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=c_GlideAggregateAPI
GlideRecordSecure is a class inherited from GlideRecord that performs the same functions as GlideRecord, and also enforces ACLs.
Thanks,
Dhananjay.
06-19-2020 12:43 AM
Hi there,
Thanks for the info. This is exactly what I explained and gave as example. 🙂
GlideAggregate does not have documentation on how to be used with row level ACLs and the getRowCount() will give wrong count when used with GlideRecordSecure().