The CreatorCon Call for Content is officially open! Get started here.

User criteria on a Portal Widget

Dale Bersane
Tera Contributor

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();

})();

7 REPLIES 7

Slava Savitsky
Giga Sage

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().

@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?

Anurag Tripathi
Mega Patron
Mega Patron

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. 

-Anurag

@Anurag Tripathi It is a reference field, on my custom table, to the sys_user table.