Limit Select Box variable based on group

Cirrus
Kilo Sage

Hi,

We have a select box variable in a catalog item which has 3 choices, A,B,C. If the logged in user is a member of Group X, then they can see all 3 choices, but if they are a member of any other group, they should only see A and B. 

I tested the principle using the following role based on load Client Script, which works exactly as we need it:

function onLoad() {
var isAdmin = g_user.hasRole('admin');
if (!isAdmin){
//alert('Current user is not an admin');
g_form.removeOption('variable','C');
}
}

My question is how to turn this into a group based condition. I tried the following, but the gs object should not be used in client scripts

function onLoad() {
var currentUser = gs.getUser();
if (currentUser.isMemberOf('X')){
g_form.addOption('variable','C');

Any help gratefully received.

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Cirrus,

Create a Script Include and call it from Client Script.

Variable choices defined as below.

find_real_file.png

Script Include

var UserInfoClient = Class.create();
UserInfoClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isUserInGroup: function() {
        return gs.getUser().isMemberOf('<name of group>').toString();
    },
    type: 'UserInfoClient'
});

Client Script

function onLoad() {
	var ajax = new GlideAjax('UserInfoClient');
    ajax.addParam('sysparm_name', 'isUserInGroup');
	ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0 && answer == 'true') {
            g_form.addOption('variable', 'C', 'C');
        }
    });
}

Execution result

case 1:when user is a member of a group

find_real_file.png

case 2: when user is not a member of a group

find_real_file.png

 

View solution in original post

17 REPLIES 17

To clarify, I built a standalone item with one select box variable called What, then created two choices A and B as per you reply.

I then created the script include, selected client callable, and updated the group to the correct name and added myself to the group.

Finally, I added an On Load catalog client script, UI Type is All, and updated the last line of the script to g_form.addOption('what', 'C', 'C') where what is the Select Box name value

However, this does not return C in either the client or the portal.

As is in the other questions, are scope of both Script Include and Maintain Item form set to Global or to the same scope name?

They are all set to Global:

Script include

find_real_file.png

Try running the following script in Scripts - Background.

var userInfo = new UserInfoClient();
var isInGroup = userInfo.isUserInGroup();
gs.info(isInGroup);

It should return "true".

[0:00:00.107] Script completed in scope global: script
Script execution history and recovery available here
*** Script: true

If it returns "false", check if the current user is a member of "Digital Environments" group. I'm not certain from the screenshot but check that there is no space at the end of "Digital Environments ".

I have my current logged in user be a member of group "Team Development Code Review".

find_real_file.png

So my Script include is as follows.

    isUserInGroup: function() {
        return gs.getUser().isMemberOf('Team Development Code Reviewers').toString();
    },

One thing that might be causing issues here is that your Name is set to "Get Group Member", but that doesn't match your class name or the type property on the script include, try and change the Name field to match the class name: UserInfoClient