Populate choice list options based on logged in user group membership

benrollins
Kilo Expert

I have a choice list which is populated by a reference field, which has quite a lot of options.
I would like to be able to reduce the number of options based on the group membership of the logged in user.

Ex:
If I have 10 values populating this choice list, and a user who is a member of group X selects the dropdown, they will only see the first 5.
A user who is a member of group Y will only see the last 5 options.
A user who is a member of both groups will see all 10 options.

I believe I will need to use an On Load client script, but have not found a way to query for a group membership of the current user, and restrict (or show) the options accordingly.

1 ACCEPTED SOLUTION

It will allow multiple group member values. This can be achieved.

g_scratchpad.memberCapex = true;
g_scratchpad.memberAdmin = true;

I haven't used the scratchpad much yet, am I able to define multiple results?  In the BR you referred to g_scratchpad.member but the Client Script if statment just used g_scratchpad.--  Sorry for the typo it should be g_scratchpad.memberCapex = true; in the client script too


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

10 REPLIES 10

ServiceNowSteve
Giga Guru

Try this client script:

function onLoad() 
{
   var checkRole = g_user.hasRole('USER_ROLE_HERE');
   var choices = g_form.getValue('CHOICE_LIST_NAME');

   if (checkRole == true)
   {
      g_form.removeOption('choices', 'VALUE_TO_REMOVE');
      g_form.removeOption('choices', 'VALUE_TO_REMOVE_2');
      g_form.removeOption('choices', 'VALUE_TO_REMOVE_3');

   }
}

Found that the following line doesn't really do anything:

var choices = g_form.getValue('CHOICE_LIST_NAME');

I removed it, and changed 'choices' of the script to the name of the field on my form, using the sysID of the values.  They were removed.

Prateek kumar
Mega Sage

You would require a Display Business rule to store group membership of current logged in user value(using scratchpad value) and use that scratchpad value in your onLoad Client script.

Your script for Display BR should be something like this.

// Add your code here
if(gs.getUser().isMemberOf('Capex Approvers')||gs.getUser().isMemberOf('Technology Time Management Admin')){
g_scratchpad.member = true;
}
else{
g_scratchpad.member = false;

 

And OnLoad client script:

if (g_scratchpad== true)
{
g_form.removeOption('choices', 'VALUE_TO_REMOVE');
g_form.removeOption('choices', 'VALUE_TO_REMOVE_2');
g_form.removeOption('choices', 'VALUE_TO_REMOVE_3');

}
}


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Hi Prateek,

I haven't used the scratchpad much yet, am I able to define multiple results?  In the BR you referred to g_scratchpad.member but the Client Script if statment just used g_scratchpad.

Am I able to define multiple results on the scratchpad, such as:

g_scratchpad.memberCapex = true;
g_scratchpad.memberAdmin = true;

Then call those results in separate IF statements on the client script?
I'm still a bit hung up on how to deal with the scenario where a user is a member of both groups, and as a result shouldn't lose any options. 

I'm thinking a nested IF statement might do the trick if the scratchpad will let me pass multiple group member results at the same time.