getMyGroups() not working properly.

KS86
Tera Contributor

Hi all,

 

I am using getMyGroups() in my code to get all groups. It is returning one extra group. Why?

KS86_0-1766504083110.png

 

I know below thing can be achieved by other ways, but I want to use getMyGroups().

 

My code-

var groups = gs.getUser().getMyGroups();
var groupIds=JSON.parse(JSON.stringify(groups));
var gr=new GlideRecord('sys_user_group');
gr.addQuery('sys_id', 'IN', groupIds);
gr.query();

while(gr.next()){
    gs.info("Name "+gr.name)
}

Answer-
*** Script: Name HR Tier 1
*** Script: Name App Engine Admins
*** Script: Name Conditional Script Writer
*** Script: Name Team Development Code Reviewers
*** Script: Name HR
1 ACCEPTED SOLUTION

@KS86 Below will return expected output:

 

gs.getUser().getMyExplicitGroups()

Raghav
MVP 2023
LinkedIn

View solution in original post

10 REPLIES 10

@KS86 

this is not documented but members in past have reported the same OOTB behavior and hence we used that approach

this past post talks about the same

getMyGroups() function is returning any parent group of child group in bread crumb 

this KB talks about same when the same function is used in dynamic filter options

User is able to see a list of records from the groups that user is a part of and also the groups whi... 

AnkurBawiskar_0-1766557339569.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@KS86 

I believe I also shared a working solution using GlideRecord.

As per new community feature you can mark multiple responses as correct.

💡 If my response helped, please mark it as correct as well so that this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

RaghavSh
Mega Patron

@KS86 this function is returning parent group as well.

There is another OOB SN function "getMyExplicitGroups()" , this will give you the output you are expecting.


Raghav
MVP 2023
LinkedIn

@KS86 Below will return expected output:

 

gs.getUser().getMyExplicitGroups()

Raghav
MVP 2023
LinkedIn

KS86
Tera Contributor

Hi @RaghavSh ,

 

Now It is giving me exact groups.

But when I try to use that in Gliderecord it is not giving me exact result. It is giving four sys_ids but when I try to iterate through that it is giving only 2 groups.
Script -

var groups=gs.getUser().getMyExplicitGroups();
gs.info(groups);
var gr = new GlideRecord("sys_user_group");
gr.addQuery("sys_id","IN", groups);
gr.query();
while (gr.next()) {
   gs.info(gr.name);
}
Output-
** Script: [22d52c269f221200d9011977677fcf97, 477a05d153013010b846ddeeff7b1225, 5ee74940b70022108d4406dd1e11a918, cfcbad03d711110050f5edcb9e61038f]
*** Script: App Engine Admins
*** Script: Conditional Script Writer