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

PFA

Your groups field, I hope is also list collector field referring to sys_user_group table.

It should work just fine

Best Regards
Aman Kumar

Vishal Birajdar
Giga Sage

Hello @Revathi12 

 

Can you try like below :

 

Step 1 : Create Script include (Do not make client callable);

/*Get Groups which user is member of*/
 isMemeberOfGroups: function() {
        var userId = gs.getUserID();
        var groups = [];
        var grMem = new GlideRecord('sys_user_grmember');
        grMem.addQuery('user=' + userId);
        grMem.query();
        while (grMem.next()) {
            groups.push(grMem.getValue('group'));
        }

        return 'sys_idIN' + groups.toString();    
    },

/*Get Groups which user is not member of*/
isNotMemeberOfGroups: function() {
        var userId = gs.getUserID();
        var groups = [];
        var notPartOfGroups = [];
        var grMem = new GlideRecord('sys_user_grmember');
        grMem.addQuery('user=' + userId);
        grMem.query();
        while (grMem.next()) {
            groups.push(grMem.getValue('group'));
        }

        var grGroup = new GlideRecord('sys_user_group');
        grGroup.addQuery('sys_idNOT IN' + groups.toString());
        grGroup.query();

        while (grGroup.next()) {
            notPartOfGroups.push(grGroup.getUniqueValue());
        }

		return 'sys_idIN' + notPartOfGroups.toString();

    },

 

 

VishalBirajdar_4-1708584389072.png

 

 

 

Step 2 : Add reference qualifier in Variable 'Existing groups'

This will return the list of existing groups

javascript: new CheckGroupMembership().isMemeberOfGroups();

//Please see the screenshot for format

 

VishalBirajdar_1-1708583754334.png

 

Step 3 : Add below reference qualifier in variable "New Groups"

javascript: new CheckGroupMembership().isNotMemeberOfGroups();

 

VishalBirajdar_5-1708584436646.png

 

 

This will return the groups which user is not part of

 

Output : 

For me Admin is only part of two groups 

 

VishalBirajdar_3-1708583953858.png

 

and not part of below groups

 

VishalBirajdar_6-1708584478950.png

 

 

 

 

Vishal Birajdar
ServiceNow Developer

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

Hi @Vishal 

Thanks for the response. It is not working for existing groups, whatever the user I select it is showing the same groups.

Hi @Revathi12 

 

Have you updated the same script include which you have previously used...??

 

Can you try to create new script include and then check....!!

 

 

Vishal Birajdar
ServiceNow Developer

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