I have a public group and many Portal Roles added to that along with other users. How do I query all the portal roles added to this public group in SOQL ?
1 Answer
I am assuming you know the Group Id(lets say your group name is MyCoolUsers
). If not, you can query on Group
object against the developername
of the Group.
Next, you need to query on GroupMember
object and find out which all groups are part of this Group. Yes, the roles are stored as Groups in GroupMember
. Bear with me, I will explain.
SELECT UserOrGroupId FROM GroupMember where GroupId = :groupId
This will return both UserIds
and GroupIds
. So you need to pass it through a for loop and catch only the ones starting with 00G
(keyPrefix of Group object)
Example of query result:
The ones starting with 00G
are the ones you need.
for(GroupMember gm : [SELECT UserOrGroupId FROM GroupMember where GroupId = :groupId])
{
if(gm.UserOrGroupId.startsWith('00G'))
roleGroupIdList.add(gm.UserOrGroupId);
}
Now you have a list of groupIds which belongs to your actual group, MyCoolUsers
.
Next, fire a SOQL on Group object against the Id matching with roleGroupIdList
.
SELECT Id,RelatedId FROM Group where Id IN :roleGroupIdList
You see the RelatedId
? That field holds the actual Portal Role Ids you want.