Can I restrict selection of an option on a select box variable to certain role?

Hola Ola
Giga Guru

Hello all, 

I am looking for a way, if there's any, to have the options in a select box be selectable by role. 

 

As shown in he attachment, I would like for only users with certain role OR group membership to ONLY be able to select (or see) the Purchase option. 

 

HolaOla_0-1721047880492.png

Any help or suggestion would be appreciated.

 

Thanks

 

1 ACCEPTED SOLUTION

Hey, 

it should be like this

function onLoad() {
  if (!g_user.hasRole('rtls_purchase_view')) {
        g_form.removeOption('type_of_purchase_replacement',"Purchase");
    }
}

, also remove <>, those are just to show where you have to place the fields

 

-Anurag

View solution in original post

7 REPLIES 7

Hey, 

it should be like this

function onLoad() {
  if (!g_user.hasRole('rtls_purchase_view')) {
        g_form.removeOption('type_of_purchase_replacement',"Purchase");
    }
}

, also remove <>, those are just to show where you have to place the fields

 

-Anurag

vishakhayadav24
Tera Guru

Hi @Hola Ola ,

 

you can write a script to check if the user is  holding that particular role  then should be able to see the option else use the removeOption for removing the Purchase option from dropdown list.

Robbie
Kilo Patron
Kilo Patron

Hi @Hola Ola,

 

This is easily achieved by using a Client Script and check if the logged in user has the role (or is a member of the group).

I've provided a link to the ServiceNow docs which go into more details, but essentially the g_user API is available from Client Scripts and you leverage the following method:

 

if (!g_user.hasRoleExactly('the_role_name_required')) {
        g_form.removeOption('the_field_name', 'the_value_name_to_remove');
}​

 

 

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


Thanks, Robbie

 

Here's the SN link: https://developer.servicenow.com/dev.do#!/learn/courses/washingtondc/app_store_learnv2_scripting_was...