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

@Revathi12 

In the advanced reference qualifier, you can just write :

javascript: "sys_idNOTIN"+current.variables.existing_group;//validate the field name for existing group field

Best Regards
Aman Kumar

Hi Aman,

I have added condition javascript: "sys_idNOTIN"+current.variables.groups; (groups is the name of the variable) but its not working.

There is a space between NOT and IN

something like

"sys_idNOT IN"+current.variables.groups;

Best Regards
Aman Kumar

Updated but still the same issue

Can you share the screenshot of the changes that you made

Best Regards
Aman Kumar