function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JayaJayaJayaJaya 

How to get the Group Name/Group Id of currently logged in user in Apex?

Hi,

 

Please let me know how to get the Group Name/Group Id of currently logged in user in Apex?

 

Thanks in advance.

digamber.prasaddigamber.prasad

Hi,

 

You can use below 2 line of codes:-

 

Id currentUserId = UserInfor.getUserId();
List<GroupMember> lstGroupMember = [SELECT GroupId,UserOrGroupId FROM GroupMember where userorgroupid =:currentUserId];

 

Query in second line will give you list of GroupMember, from this list you can get IDs of all Group and query Group object to get detail of group.

 

Hope it will help you!

 

Happy to help you!

 

Regards,

Digamber Prasad

 

 

JayaJayaJayaJaya
Thanks Digamber for the reply, but could not understand how to fetch the Group Name yet, please help..
digamber.prasaddigamber.prasad

Hi,

 

I have modified my above code snippet

 

Set<Id> setGroupMemberId = new Set<Id>();
List<CollaborationGroup> lstGroup = new List<CollaborationGroup>();
Id currentUserId = UserInfor.getUserId();
List<GroupMember> lstGroupMember = [SELECT GroupId,UserOrGroupId FROM GroupMember where userorgroupid =:currentUserId];
for(GroupMember gm : lstGroupMember){
	setGroupMemberId.add(gm.GroupId);	//this set will give you set of all Groups
}

if(setGroupMemberId.size() > 0)
	lstGroup = [SELECT Id,Name FROM CollaborationGroup where Id in: setGroupMemberId];	//this list will give you list of all group records

 

Hope it will help you!

 

Happy to help you!

 

Regards,

Digamber Prasad