User Criteria on custom table

Ken83
Mega Guru

Hello Community,

 

I was referencing this article → https://community.servicenow.com/community?id=community_question&sys_id=44738b25dbd8dbc01dcaf3231f961961&view_source=searchResult ← for guidance on using 'User Criteria' records with a custom table. 

I have a created a table with roles in it. This table will be referenced by a List Collector variable on a catalog item. I want to utilize User Criteria records on this custom table. If the user viewing the catalog item does not meet the User Criteria, the records from the custom table do not show as options in the List Collector variable. 

How can I accomplish this behavior? Again, referencing the article above, I can retrieve the User Criteria and determine if the user meets the criteria or not. But how do I show/hide records on that custom table(and in the List Collector variable) if the user does not meet the criteria?

1 ACCEPTED SOLUTION

Ken83
Mega Guru

I was able to find a solution to this. Utilizing the code outline in the article above, I'm able to retrieve a Java ArrayList object that contains sys_ids of the User Criteria records that the current user satisfies. Since it is not a native Javascript object, don't bother trying typical Array methods on it, won't behave as expected. Instead, I had to get the size of the ArrayList using the .size() method(equivalent of JavaScript .length() method) in a FOR loop. As I looped through that ArrayList object, I pushed each value out into a native JavaScript array using nativeJSArray.push(ArrayList.get(x)). Once I had the nativeJSArray populated, I just returned that to a before query Business Rule. Since it's a custom table, I added a reference field to reference the User Criteria table so that I could specify User Criteria for each row in the custom table. In the Business Rule, I just modified the query to check that the criteria returned, matched one of the criteria records identified on the records. Problem solved. Hopefully this is useful to someone looking to do the same. This will be something to re-visit after upgrades though to make sure ServiceNow hasn't deprecated the code outlined in that article.

View solution in original post

5 REPLIES 5

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Ken, this sounds complicated to use user criteria.  Do you expect the criteria to change often?  ACLs maybe an easier way to manage this in combination with a reference qualifier.

Michael, thank you for that suggestion. Yes, I expect these criteria to change often or to be added often. We will have several groups/departments with different criteria added at different times. I initially started down this road with ACLs but thought the User Criteria would be easier. 

Taking the path of using ACLs, I'll probably have to extend that to a script include so that I can make it data-driven to accommodate all the changes/additions of user criteria. Basically, the idea i had was to use the script field of the ACL to utilize the script include. The script include will reference the User Criteria records so that I can determine if the user matches the associated criteria record.

The issue I ran into with this approach is that I can't seem to get the Read ACL to restrict the record in the custom table list view if the ACL script returns false. Sounds simple, but i'm not sure what i missed. My assumption is that if the ACL is restricting 'Read' access to that row in the table, then from a catalog item(list collector variable), that row won't be visible which is exactly what I want.

Ken83
Mega Guru

This is the script include I am using in the ACL script...

find_real_file.png

 

This is the ACL script...

find_real_file.png

 

The User Criteria record is checking for a specific location. The user I am testing with does not meet the criteria, but can still see the record in the list view.

find_real_file.png

Ken83
Mega Guru

I was able to find a solution to this. Utilizing the code outline in the article above, I'm able to retrieve a Java ArrayList object that contains sys_ids of the User Criteria records that the current user satisfies. Since it is not a native Javascript object, don't bother trying typical Array methods on it, won't behave as expected. Instead, I had to get the size of the ArrayList using the .size() method(equivalent of JavaScript .length() method) in a FOR loop. As I looped through that ArrayList object, I pushed each value out into a native JavaScript array using nativeJSArray.push(ArrayList.get(x)). Once I had the nativeJSArray populated, I just returned that to a before query Business Rule. Since it's a custom table, I added a reference field to reference the User Criteria table so that I could specify User Criteria for each row in the custom table. In the Business Rule, I just modified the query to check that the criteria returned, matched one of the criteria records identified on the records. Problem solved. Hopefully this is useful to someone looking to do the same. This will be something to re-visit after upgrades though to make sure ServiceNow hasn't deprecated the code outlined in that article.