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
Tera Contributor

It is working fine, if variable type is single line/Multi line text. Issue is when variable type is list collector

Aman Kumar S
Kilo Patron

Hi @Revathi12 

Change the code as below:

groups1.push(gr1.getValue('group'));

List collector is the field that contains string of sys_ids and can't access display name of the group, so you have to make it work by using the sys_id instead of display name.

 

Also, one suggestion, instead of doing this validation on client script, why don't you create a reference qualifier on the "Add new group" field, which only shows the list of group which is not in "Existing group" list.

Let me know your thoughts on this.

Best Regards
Aman Kumar

Hi @Aman Kumar S 

 

Thanks for the response, I tried but still the same issue.

 
 

Can you pls help me with reference qualifier condition.