User criteria on a Portal Widget
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2024 06:45 AM
I have a requirement to restrict visibility to specific portal widgets based on user criteria.
I need to check if the current logged in user has a checkbox marked true or false, on a custom table, and if true allow visibility to the widget. I have verified the user i am using to test with has access to the custom table and can read all fields.
I wrote the following script, within the user criteria for who can view this instance, but it didn't work. Any help would be appreciated:
(function() {
// Get the logged-in user's sys_id
var userID = gs.getUserID();
// Query the custom table to see if the user has the true checkbox
var gr = new GlideRecord('u_training'); // custom table
gr.addQuery('u_name', userID); // reference field, on my custom table, to the sys_user table
gr.addQuery('u_checkbox', true); // checkbox field on custom table
gr.query();
// Return true if the user has the true checkbox, otherwise return false
return gr.hasNext();
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2024 07:18 AM
Make sure you follow the following recommendations from this product documentation article:
- The script is evaluated in the scope that the user criteria is created in.
- The evaluation of the script is cached in the session, so any change in the evaluation requires you to logout and login, similar to roles in ACL.
- Do not use gs.getUser() or other session APIs since they cause conflict when used in diagnostic tools. Use the pre-defined user_id variable available in the script to get the user id of the user being used to evaluate the script.
- Because scripts are evaluated dynamically, including scripts in user criteria records can decrease performance.
- Because answer is a pre-reserved keyword, do not use a function with its name as answer, that is, answer().
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2024 08:04 AM
@Slava Savitsky One of the points indicates "Because scripts are evaluated dynamically, including scripts in user criteria records can decrease performance.". Is there another way you would recommend?
And for this point "Do not use gs.getUser() or other session APIs since they cause conflict when used in diagnostic tools. Use the pre-defined user_id variable available in the script to get the user id of the user being used to evaluate the script." have you scripted this way for user criteria?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2024 07:28 AM
What type of field is u_name on on u_training table?
If it is a reference then to what table?
Also please try to use a different variable name instead of gr.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2024 08:00 AM
@Anurag Tripathi It is a reference field, on my custom table, to the sys_user table.