Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to getMyGroups in scoped application

Komal_D
Kilo Contributor

Hi,

How to get my all groups in scoped application.

I am getting error below

Server JavaScript error Cannot find function getMyGroups in object com.glide.script.fencing.ScopedUser@1ac4a90.

 

I am writing an encoded query to get my group is there any way to solve above error and get my groups.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Komal,

Have this custom function to get the groups for the user:

var uID = gs.getUserID();

var groupArr = [];

var grmember = new GlideRecord('sys_user_grmember');
   grmember.addQuery('user', uID);
   grmember.addQuery('group.active', true);
   grmember.query();
   while(grmember.next()){
      //Push the group sys_id values into the group array
      groupArr.push(grmember.group.toString());
   }

the array when you print will tell you the groups to which user belongs.

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards
Ankur

View solution in original post

8 REPLIES 8

Gurpreet07
Mega Sage

Write your own script include in specific application scope. GlideRecord  sys_user_gr_member table to get comma separated list of groups user belongs too.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Komal,

Have this custom function to get the groups for the user:

var uID = gs.getUserID();

var groupArr = [];

var grmember = new GlideRecord('sys_user_grmember');
   grmember.addQuery('user', uID);
   grmember.addQuery('group.active', true);
   grmember.query();
   while(grmember.next()){
      //Push the group sys_id values into the group array
      groupArr.push(grmember.group.toString());
   }

the array when you print will tell you the groups to which user belongs.

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards
Ankur

Thanks Ankur, this solved my issue

Hi,

Ankur's solution isn't exactly the same behaviour of getMyGroups() as that function also retrieves the ancestor groups of the user - all parent groups of the groups the user is a member of.

I've adapted Ankur's solution to also retrieve all parent groups to provide a like-for-like replacement for the global function. Happy to receive suggestions on how to make it more efficient!

function getMyGroups() {
	var groups = [];
	var grmember = new GlideRecord('sys_user_grmember');
	grmember.addQuery('user', gs.getUserID());
	grmember.addQuery('group.active', true);
	grmember.query();
	while(grmember.next()){
		groups.push(grmember.getValue('group'));
	}
	// add all parent groups (this is global.getMyGroups behaviour)
	var children = groups;
	while(children.length > 0) {
		var grp = new GlideAggregate('sys_user_group');
		grp.addQuery('active', true);
		grp.addQuery('sys_id', 'IN', children.join(','));
		grp.addEncodedQuery('parentISNOTEMPTY');
		grp.groupBy('parent');
		grp.query();
		children = [];
		while(grp.next()) {
			var parent_id = grp.getValue('parent');
			if (groups.indexOf(parent_id) == -1) {
				groups.push(parent_id);
				children.push(parent_id);
			}
		}
	}
	return groups;
}

All the best,
Tim