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

-O-
Kilo Patron
Kilo Patron

It would be easier to use Lookup select box coupled with a reference qualifier; those run server side too, where you could make use of gs methods.

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

 

Thanks Hitoshi, but this is still giving me a 'Javascript error in your browser console' in the Service Portal view and isnt working

Is there still JavaScript error in browser console?

I'm using Chrome and Firefox. ServiceNow no longer supports Edge so shouldn't be using it. IE isn't supported in Workspace either.

Supported are Chrome, Firefox, MS Edge Chromium, Safari.

https://docs.servicenow.com/bundle/rome-release-notes/page/administer/navigation-and-ui/reference/br...