Restrict a select box choice to specific group on catalog

Joshuu
Kilo Sage

Hi All,

 

I have a catalog form and a select box with 3 choices in it. Example: A,B,C.

 

Only 3 particular group members should be able access 'A' choice. If any user try to select from the drop down it should clear that value with an alert or that choice itself should not be visible to any other user apart from those 3 groups.

 

And we do not have any variable as assignment group on form. It should check the requester/user's group.

 

Please assist.

 

Thanks & Regards.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

It's a much better user experience to remove the choice rather than allow it to be selected, but then present an error in some cases.  You can do this with an onLoad Catalog Client Script which uses GlideAjax to call a Script Include

 

function onLoad() {
    var ga= new GlideAjax("CatalogUtils"); //Script Include name
    ga.addParam("sysparm_name", "getGroupMembership"); //name of function
    ga.addParam("sysparm_user", g_user.userID);
    ga.getXML(processResponse);

    function processResponse(response){
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'false'){ //current user is not a member of one of the 3 groups
            g_form.removeOption("var_name","choice_value"); //replace with your variable name and choice value to be removed
        }
    }		
}

 

Your Client callable Script Include would look something like this:

 

var CatalogUtils = Class.create();
CatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
		
	getGroupMembership: function(){
		var answer = 'false';
		var usr = this.getParameter("sysparm_user");
		
		if (gs.getUser(usr).isMemberOf('group name 1')) { //replace with your group name
			answer = 'true';
		} else if (gs.getUser(usr).isMemberOf('group name 2')) { //replace with your group name
			answer = 'true';
		} else if (gs.getUser(usr).isMemberOf('group name 3')) { //replace with your group name
			answer = 'true';
		}
		
		return answer;
	},
		
    type: 'CatalogUtils'
});

 

View solution in original post

11 REPLIES 11

It's likely that sysparm_user, hence usr is blank so the gs.getUser is defaulting to the current user.  In your testing of this script, are you first selecting a Requested for value?  The script should not run if this variable is empty.  Have you confirmed that the Requested for variable name is 'requestor' or is it 'requested_for' or something else?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
alert('Requested for = ' + g_form.getValue('requestor'); //temporary to confirm value passed to Script Include
    if (g_form.getValue('requestor') != '') {   
        var access = g_form.getValue('access_role');
        var ga = new GlideAjax("getUserGroup"); //script include name
        ga.addParam("sysparm_name", "getgrp"); // script include function name
        ga.addParam("sysparm_user", g_form.getValue('requestor'));
        ga.getXML(processResponse);

        function processResponse(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
            if (access == 'admin') {
                if (answer == 'false') { //if user is not part of the 2 groups
                    alert('You cannot request for Administrator Role');
                    g_form.clearValue("access_role", true);
                }
            }
        }
    }
}

Once this is working, you'll also need a similar script onChange of Requested for.

Hi @Brad Bowman  ,

 

Thank you for the response. 

Have you confirmed that the Requested for variable name is 'requestor' or is it 'requested_for' or something else?

Yes, the name of the variable is requestor.

 

I will try the above script.

 

 

Hi @Brad Bowman ,

 

Yes, I am getting the sys_id of the user value in the alert which is on Requested For field.

Do we need to write the similar script for the requested_for too ?

 

Please suggest.

 

Thanks & Regards.

Hi @Brad Bowman  ,

 

Could you please help me for Requested For script too.

 

Thanks & Regards.

In this case you can use the same script, but with the first if statement you don't need to check for a blank value, and you can use newValue instead of g_form.getValue in the user parm - either works.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var access = g_form.getValue('access_role');
    var ga = new GlideAjax("getUserGroup"); //script include name
    ga.addParam("sysparm_name", "getgrp"); // script include function name
    ga.addParam("sysparm_user", newValue);
    ga.getXML(processResponse);

    function processResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (access == 'admin') {
            if (answer == 'false') { //if user is not part of the 2 groups
                alert('You cannot request for Administrator Role');
                g_form.clearValue("access_role", true);
            }
        }
    }
}