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

AnirudhKumar
Mega Sage

Hello Sandy,

The variable variablesToHide is not an array here, so variablesToHide.length and variablesToHide[i] are not going to give the results we need.

Replace your checkGroups function with below code and verify if the core logic works:

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
{
             g_form.setDisplay('request_type', false);
}   

  }

Hello, thanks so much for your help. I tried this and still no success. Any ideas?

Some points here:

- Is the field supposed to hide and not hiding?

- Did you alert out the answer variable and check if you are getting true and false based on if the user is part of the group?

function checkGroups(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
        
if(answer == 'false')//If logged in user IS NOT part of the group
{
             g_form.setDisplay('request_type', false);
}   

  }

 

3. Try replacing 

g_form.setDisplay('request_type', false);

with

g_form.setVisible('request_type', 'false');

 

4. Is the group (97209ba71bad5d58142dc9192a4bcbff) active? Try giving the name of the group, (silly, but try)

Hello! thanks for helping. So I updated accordingly, and the alert gives me "false" during testing - so it does show the logged in user is not part of that group. 

And you are correct - the request_type field is supposed to be hidden if the user is not part of that group but that is not working. I also confirmed the group is active. 

 

Thanks!