Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

gs.getUser().isMemberOf() question

Bill Wulff
Tera Guru

Hello All,

 

I am perplexed. I am not able to return a list of groups where the current logged in user is member of the 'parent' field of the sys_user_group table. 

 

Task:

Find all groups where the 'parent' is field is not null.

Find all groups the current logged in user is a member of.

Return a list of groups where the logged in user is member of any 'parent' group.

 

I am able to return groups if I hard code them in, but not when I use gr.name or gr.sys_id.

Example: 

if(gs.getUser().isMemberOf(gr.name)) --- Does not work
if(gs.getUser().isMemberOf('Hard Code Name Here')) Does work

 

Calling script include in a advanced reference qualifier of a variable on a catalog item.
javascript:new global.getGroups().GetUserGroups();

 

Script Include:

var getGroups = Class.create();
getGroups.prototype = {
    initialize: function() {
    },	
	GetUserGroups : function()
	{
		var usersGroupList = [];
				
		var gr = new GlideRecord('sys_user_group');
		gr.addNotNullQuery('parent');
		gr.query();
		while(gr.next())
		{
			if(gs.getUser().isMemberOf(gr.name))
			{
				usersGroupList.push(gr.getUniqueValue());
			}
			gs.log('-------------' + usersGroupList + '====' + gs.getUserDisplayName());			
		}
		return 'sys_idIN' + usersGroupList;
		
	},
    type: 'getGroups'
};

 

Thank you in advance!

 

1 ACCEPTED SOLUTION

Bill Wulff
Tera Guru

Yeah, I am an idiot. Figured it out. 
I was returning the wrong group in my initial glide record query.
These two lines fixed my issue.
var groupName = gr.parent.name;
if(gs.getUser().isMemberOf(groupName))

 

var getGroups = Class.create();
getGroups.prototype = {
    initialize: function() {
    },
	
	GetUserGroups : function(user)
	{
		var usersGroupList = [];
		
		var gr = new GlideRecord('sys_user_group');
		gr.addNotNullQuery('parent');
		gr.query();
		while(gr.next())
		{
			var groupName = gr.parent.name;
			if(gs.getUser().isMemberOf(groupName))
			{
				usersGroupList.push(gr.getUniqueValue());
			}
			gs.log('-------------' + groupName + '====' + gs.getUserDisplayName());
			
		}
		return 'sys_idIN' + usersGroupList;
		
	},
    type: 'getGroups'
};

View solution in original post

1 REPLY 1

Bill Wulff
Tera Guru

Yeah, I am an idiot. Figured it out. 
I was returning the wrong group in my initial glide record query.
These two lines fixed my issue.
var groupName = gr.parent.name;
if(gs.getUser().isMemberOf(groupName))

 

var getGroups = Class.create();
getGroups.prototype = {
    initialize: function() {
    },
	
	GetUserGroups : function(user)
	{
		var usersGroupList = [];
		
		var gr = new GlideRecord('sys_user_group');
		gr.addNotNullQuery('parent');
		gr.query();
		while(gr.next())
		{
			var groupName = gr.parent.name;
			if(gs.getUser().isMemberOf(groupName))
			{
				usersGroupList.push(gr.getUniqueValue());
			}
			gs.log('-------------' + groupName + '====' + gs.getUserDisplayName());
			
		}
		return 'sys_idIN' + usersGroupList;
		
	},
    type: 'getGroups'
};