Script to check if user is part of existing group

Revathi12
Tera Contributor

Hi Team,

 

I have a requirement where I have 2 fields existing groups and add new group. In existing group I'm populating the groups which are already mapped to user. Whereas in the field Add New I need to populate the groups which are not mapped for the user and he should select only those groups.

 

Note both are of list collector variable types. Below is the script

 

 

Script Include :-

getNewGroups: function() {
        var groups1 = [];
        var groupName = this.getParameter('sysparm_group_name');
        var gr1 = new GlideRecord('sys_user_grmember');
        gr1.addQuery('group.name', groupName);

        gr1.addQuery('user',gs.getUserID());
        gr1.query();
       while (gr1.next()) {
            
             groups1.push(gr1.getDisplayValue('group.name'));
        }
      
        return groups1.toString();

    },
 
Client Script :-
 
alert ("tst1"+newValue);
var ga = new GlideAjax('POC_RN');
ga.addParam('sysparm_name', 'getNewGroups');
ga.addParam('sysparm_group_name',newValue);
ga.getXML(getResults);

function getResults(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if(answer!=''){
    g_form.addInfoMessage("group already exists");
   g_form.clearValue('add_new');
   }
   else {
    g_form.setValue('add_new', newValue);
   }
   
}
   
20 REPLIES 20

Hi Vishal, Same script I have copied, existing groups it is showing same for every user?

Hi Revathi,

 

Can you share screenshot of script include and reference qualifier...Please...

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi Vishal,

 

PFA

All looks good....!!

It's strange...why its not working...

 

Can you put some logs in script include and check if you are getting the groups in array...!!

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

It is working for logged in user, however when we try to change requested for value its always quering logged in user only due to getUserID();