The CreatorCon Call for Content is officially open! Get started here.

Make variable visible only to users in a certain group?

Sandy7
Tera Expert

Hello,

I have a catalog item and I am trying to make a variable (request_type) visible only when the logged in user is part of a specific group (Senior Director Enterprise Group). 

I have tried doing this using a script includes as well as an on-load client script. I will post my code for both below. However, this is not working. 

Here is the script include (I have client callable selected):

var validateMember = Class.create();
validateMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateLoggedUserGroup: function() {
		var arr = [];
        var getLoggedInUser = this.getParameter('sysparm_user');
        var gr = new GlideRecord('sys_user_grmember');
		gr.addQuery('user',getLoggedInUser);
		gr.query();
		while(gr.next()){
			arr.push(gr.group.getDisplayvalidateMembere().toString());
		}
		return arr.toString();
    },



    type: 'validateMember'
});

 

Here is the On Load client script:

function onLoad() {
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('validateMember');
    ga.addParam('sysparm_name', 'validateLoggedUserGroup');
    ga.addParam('sysparm_user', g_user.userID);
    ga.getXML(checkGroups);

    function checkGroups(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var variablesToHide = ['request_type']; // Enter your variable backend Name which you want to hide
        var groupsToValidate = 'S97209ba71bad5d58142dc9192a4bcbff'; 
        if (answer.indexOf(groupsToValidate) > -1) { //Check If USer if Member of Group Or Not which you want
            for (var i = 0; i < variablesToHide.length; i++) {
                g_form.setDisplay(variablesToHide[i], false);
            }
        }

    }
}

 

I have tried the line where it shows the group sys id using both group sys id and also group name, but neither is working. Any ideas??

 

Thanks!

5 REPLIES 5

AnirudhKumar
Mega Sage

Did you try isMemberOf()

gs.getUser().isMemberOf('<<group sys id here>>');

 

thats a direct true / false output 

Thanks for the response, would that line of code be in the script includes, or the client script?

It should be in the script include.

Anything that starts with 'gs.' is server side script.

 

Script Include:

var validateMember = Class.create();
validateMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateLoggedUserGroup: function() {
           return gs.getUser().isMemberOf('<<group sys id here>>');
    },

    type: 'validateMember'
});

 

Client script:

function onLoad() {
     var ga = new GlideAjax('validateMember');
    ga.addParam('sysparm_name', 'validateLoggedUserGroup');
    ga.getXML(checkGroups);
}


 function checkGroups(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var variablesToHide = ['request_type']; // Enter your variable backend Name which you want to hide
        
if(answer == 'false')//If logged in user IS NOT part of the group
{
            for (var i = 0; i < variablesToHide.length; i++) {
                g_form.setDisplay(variablesToHide[i], false);
            }
 }   

    }

Hello thank you so much for the detailed response. It's still not working though. request_type is the backend variable name, so I have the code as follows:

  var variablesToHide = 'request_type'; I also tied ('request_type')

I am not sure what I am missing. Any ideas? thanks again.