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

Dale Bersane
Tera Contributor

@Anurag Tripathi and @Slava Savitsky so somehting like this?

(function() {
// Create a new GlideRecord object for the sys_user table
var userRecord = new GlideRecord('sys_user');

// Get the logged-in user's record using the pre-defined user_id variable
userRecord.get(user_id);

// Check if the logged-in user has the true checkbox on the custom table
var trainingRecord = new GlideRecord('u_training'); // custom table
trainingRecord.addQuery('u_name', userRecord.sys_id); // reference field, on my custom table, to the sys_user table
trainingRecord.addQuery('u_checkbox', true); // checkbox field on custom table
trainingRecord.query();

// Set the answer to true if a matching record is found, otherwise false
var hasAccess = false;
if (trainingRecord.hasNext()) {
hasAccess = true;
}

return hasAccess;
})();

Your script looks good, but instead of returning a value from the function, you need to set the global variable answer to either true or false like this:

 

 

function checkIfAccessAllowed() {

// your code goes inside the function

}

answer = checkIfAccessAllowed();

 

function checkIfAccessAllowed() {
    // Create a new GlideRecord object for the sys_user table
    var userRecord = new GlideRecord('sys_user');
    userRecord.get(user_id); // Get the logged-in user's record using the user_id variable

    // Create a new GlideRecord object for the custom table
    var trainingRecord = new GlideRecord('u_training'); // custom table

    // Add query conditions for the custom table
    trainingRecord.addQuery('u_name', userRecord.sys_id); // reference field to the sys_user table
    trainingRecord.addQuery('u_checkbox', true); // checkbox field on custom table

    // Query the records in the custom table
    trainingRecord.query();

    // Initialize the hasAccess variable
    var hasAccess = false;

    // Check if any matching record exists in the custom table
    if (trainingRecord.hasNext()) {
        hasAccess = true;
    }

    // Return the result
    return hasAccess;
}

// Set the global variable 'answer' to the result of checkIfAccessAllowed function
answer = checkIfAccessAllowed();