Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Variable visible only to people in certain groups?

Sandy7
Tera Expert

Hello,

I recently asked this question and got some feedback but unfortunately I still cannot get this to work so I am reaching out again..

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() {
           return gs.getUser().isMemberOf('97209ba71bad5d58142dc9192a4bcbff');
    },

    type: 'validateMember'
});

Here is the Client Script (I have tried request_type multiple different ways (request_type'; I also tied ('request_type')..

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);
            }
 }   

    }

Any idea how to make this work? thanks everyone!

1 ACCEPTED SOLUTION

ok, If the answer variable is giving true when the user is part of the group and vice versa then 90% of the job is done. Lets try few more things, patience pls 🙂

 

1. Replace

var answer = response.responseXML.documentElement.getAttribute("answer");

with

var answer = response.responseXML.documentElement.getAttribute("answer").toString();

 

2. Verify the back-end name of the Request Type field (request_type).

3. Try 

g_form.setDisplay('request_type', 'false'); //false with quotes

View solution in original post

8 REPLIES 8

ok, If the answer variable is giving true when the user is part of the group and vice versa then 90% of the job is done. Lets try few more things, patience pls 🙂

 

1. Replace

var answer = response.responseXML.documentElement.getAttribute("answer");

with

var answer = response.responseXML.documentElement.getAttribute("answer").toString();

 

2. Verify the back-end name of the Request Type field (request_type).

3. Try 

g_form.setDisplay('request_type', 'false'); //false with quotes

Thank you!!! this works perfectly! I think another issue was I had that field set to Mandatory - so once I removed that and replaced it with this code it works!

thanks again. you rock. 

Mahendra RC
Mega Sage

Hello Sandy,

Please use below code:

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);
            }
 }   

    }

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

 

Hello, 
thanks for your help. I tried this and it didn't work. ANything else you can think of?

thanks