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

GroupMember and User

I want to enable/disable a custom buttton on a VF page based on the Public Group (Group B, for example). 


To achieve this, I am querying GroupMember object. I check whether the UserOrGroupId field value equals current user's Id. but if the user is in, for example, Group A, and Group A is a sub-group of Group B, then my query fails since GroupMember object do not contain record with User Id in UserOrGroupId field, and it saves the Id of Group A


Also, if the Reason for Membership for User is equal to Manager of Group Member,  query doesn't return any value.


Please suggest any solution/design to overcome this problem. 


You will have to recursively look on the groups and group members to determine this result. Essentially the logic would be:


1. Get the groups that the current user belongs to

2. Get the groups that groups in Step 1 belong to

3. Repeat Step#2 until you've got to the topmost group


You might hit Governor limits doing 2&3. you might have to design this correctly to work around those. Probably fetch all groups and group members upfront store them in a map so that you can recurse through the map instead of SOQL queries.