06-26-2018 09:56 PM
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.
Solved! Go to Solution.
06-27-2018 12:02 AM
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
06-26-2018 10:05 PM
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.
06-27-2018 12:02 AM
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
06-27-2018 04:00 AM
Thanks Ankur, this solved my issue
04-26-2019 02:51 AM
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