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 1


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:

enter image description here

The ones starting with 00G are the ones you need.

for(GroupMember gm : [SELECT UserOrGroupId FROM GroupMember where GroupId = :groupId])

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .