Group Membership of a certain user

johnjosephjose
Tera Contributor

Hi, 

 

I need to have a field to select a group a certain user is included to. I only need to make this field show up the list of options of those group that user is in. 

 

Any thoughts? Thank you!

3 REPLIES 3

Astik Thombare
Tera Sage

Hi @johnjosephjose ,

 

Are you saying when group is selected then it should show only members of that group in assigned to

if yes then use advanced ref qualifier on assigned to

https://community.servicenow.com/community?id=community_question&sys_id=65d147a9db98dbc01dcaf3231f96...

 

Regards,

Astik Thombare

OlaN
Giga Sage
Giga Sage

Hi,

So basically you want an advanced query on the Group table, and input condition is which user is selected, to filter the available groups for that user.

You can create a script include for this and return an encoded query.

Something like this:

var GetUsersDetails = Class.create();
GetUsersDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getUsersGroups : function (userID){

		if (!userID){
			return ''; // no user given, return all records
		}
		else{
			var userGR = new GlideRecord('sys_user');
			if (userGR.get(userID)){
				var groupArray = [];
				var memberGR = new GlideRecord('sys_user_grmember');
				memberGR.addQuery('user', userID);
				memberGR.query();
				if (memberGR.hasNext()){
					while(memberGR.next()){
						groupArray.push(memberGR.getValue('group'));
					}
					return 'sys_idIN' + groupArray.join(',');
				}
				else{
					return ''; // no memberships found, return all records
				}
				


			}
			else{
				return ''; // invalid user, return all records
			}
		}
		
	},

    type: 'GetUsersDetails'
});

 

Anand Kumar P
Giga Patron
Giga Patron

Hi @johnjosephjose ,

You can create reference qualifier advanced on group field -

And create script include and update with below code

 

 

In refrence qulifier-javascript: new UserGroupReferenceQualifier().getReferenceQualifier(current.variables.user);
var UserGroupReferenceQualifier = Class.create();
UserGroupReferenceQualifier.prototype = {
    initialize: function () {},
 var groupQuery = '';
        var groupIds = [];
    getReferenceQualifier: function (userID) {
        var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('user', userID);
        gr.query();
        while (gr.next()) {
            groupIds.push(gr.group);
        }
        if (groupIds.length > 0) {
            groupQuery = 'sys_idIN' + groupIds.join(',');
        }
        return groupQuery;
    },
    type: 'UserGroupReferenceQualifier',
};

 

 

Thanks,

Anand