Hide Multiple Choice Field Option for Specific Users on Table Form View (not Catalog Items)

jmiskey
Kilo Sage

I feel like this should be easy, but the answer eludes me right now.  I tried searching the Community, but all the matches I found deal with Catalog Items, and I am not looking for that.  I am dealing with the Form view of a Table (so I am not in the Service Portal).

 

I have a Custom Table, and in that table, I have a Multiple Choice field that has 5 choices (options).  1 of those 5 options should ONLY be available to users have have a certain role.  So I would like to hide this choice from all users who do not have this role (on my Form).  How would I do that?

 

Thanks

1 ACCEPTED SOLUTION

Huynh Loc
Mega Sage

Hi @jmiskey ,

Use a Client Script (onLoad or onChange) to dynamically remove a specific choice from the choice list based on whether the current user has a required role.

This is the correct and supported approach for hiding individual choice values on a table form.

Example: 

 
function onLoad() {
    // Replace with your role and field name
    var requiredRole = 'x_custom.special_role';
    var fieldName = 'u_my_choice_field';
    var restrictedValue = 'restricted_option';

    // If user does NOT have the role, remove the choice
    if (!g_user.hasRole(requiredRole)) {
        g_form.removeOption(fieldName, restrictedValue);
    }
}

 

If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.

View solution in original post

5 REPLIES 5

Huynh Loc
Mega Sage

Hi @jmiskey ,

Use a Client Script (onLoad or onChange) to dynamically remove a specific choice from the choice list based on whether the current user has a required role.

This is the correct and supported approach for hiding individual choice values on a table form.

Example: 

 
function onLoad() {
    // Replace with your role and field name
    var requiredRole = 'x_custom.special_role';
    var fieldName = 'u_my_choice_field';
    var restrictedValue = 'restricted_option';

    // If user does NOT have the role, remove the choice
    if (!g_user.hasRole(requiredRole)) {
        g_form.removeOption(fieldName, restrictedValue);
    }
}

 

If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.

That solution sort of works.  It works when the record is viewed in normal Form view, but does not seem to work when the record is viewed in Workspace view.  Is there some way to get it to work in both views?

 

I would actually prefer that the user not use Workspace view at all.  However, I find that sometimes the record opens in Workspace view for the user when they get to it from the Global search box.  And this only seems to happen to certain users.  Really odd... I am not sure why sometimes ServiceNow opens it in Form view and other times in Workspace view.  I cannot seem to determine the logic/pattern ServiceNow uses to determine which view to open it in.

Ankur Bawiskar
Tera Patron

@jmiskey 

your UI type should be ALL

Did you check that?

share your script config screenshot

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Ha!  Yes!  That is the piece I was missing.  Once I updated that setting, it worked in Workspaces too.

 

Big thanks to the both of you for helping me!