Trying to limit visibility of a catalog item using user criteria script

afielden
Tera Contributor

I'm attempting to restrict the visibility of a catalog item with a user criteria script. I've followed the example given in this question

 

Solved: How to restrict catalog item for user country - ServiceNow Community

 

However the presence of the script makes the item invisible to everyone except admin user. It doesn't seem to make a difference what I put in this script. I've also tried running the script the debugger with a breakpoint, and the breakpoint is never hit. 

 

Is there any other logging/tracing I can use to find out what's going on?

 

6 REPLIES 6

Robbie
Kilo Patron
Kilo Patron

Hi @afielden,

 

So as to help you and provide a good starting point, can you expand on how you want to restrict the catalog item in your instance? Is it by user location (as per the example link), role, or group?

 

I'll then be able to provide you with a suitable script to use and test against.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

afielden
Tera Contributor

Hi Robbie,

Thanks for responding. I wish to hide the CI, based on the user's parent company. Here's what I've tried:

 

(function() {
    // Get the current user ID from the session
    //var userId = gs.getSession().getProperty('sys_user_id');
    var user = new GlideUser(user_id);
 
    // Get the company record for the current user
    var company = user.getCompanyRecord();
    if (company) {
        // Retrieve the parent company name (assuming it's stored in a field called 'parent_company')
        var parentCompanyName = company.parent_company.name.toString();
 
        // Define the parent company name to match against
        var targetParentCompanyName = "Mycompany"; 
 
        // Check if the parent company name matches the target parent company name
        if (parentCompanyName === targetParentCompanyName) {
            // Grant access to the catalog item(s) based on the parent company name
            return true; // Return true to grant access
        }
    }
 
    // Default: Deny access
    return false;
})();

afielden
Tera Contributor

 

(function() {
    // Get the current user ID from the session
    //var userId = gs.getSession().getProperty('sys_user_id');
    var user = new GlideUser(user_id);
 
    // Get the company record for the current user
    var company = user.getCompanyRecord();
    if (company) {
        // Retrieve the parent company name (assuming it's stored in a field called 'parent_company')
        var parentCompanyName = company.parent_company.name.toString();
 
        // Define the parent company name to match against
        var targetParentCompanyName = "Mycompany"; 
 
        // Check if the parent company name matches the target parent company name
        if (parentCompanyName === targetParentCompanyName) {
            // Grant access to the catalog item(s) based on the parent company name
            return true; // Return true to grant access
        }
    }
 
    // Default: Deny access
    return false;
})();

Hi Robbie,

 

I tried the above script. I'm trying to restrict visibility based on the user's parent company.

 

afielden
Tera Contributor

It's based on the user's parent company

 

(function() {
    // Get the current user ID from the session
    //var userId = gs.getSession().getProperty('sys_user_id');
    var user = new GlideUser(user_id);
 
    // Get the company record for the current user
    var company = user.getCompanyRecord();
    if (company) {
        // Retrieve the parent company name (assuming it's stored in a field called 'parent_company')
        var parentCompanyName = company.parent_company.name.toString();
 
        // Define the parent company name to match against
        var targetParentCompanyName = "Mycompany";
 
        // Check if the parent company name matches the target parent company name
        if (parentCompanyName === targetParentCompanyName) {
            // Grant access to the catalog item(s) based on the parent company name
            return true; // Return true to grant access
        }
    }
 
    // Default: Deny access
    return false;
})();