1

Here I have different public groups,Now i want to create one visual force page with one text box. thank you in advance.

1
  • 2
    Suresh, please take the time to visit the Help center and read How do I ask a good question. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you. SFSE is a Q&A forum where we look to help each other by asking and answering questions about real problems or errors with our code. Posting requests to write code for you generally get closed. – crmprogdev Oct 21 '15 at 13:21
1

As far as I understand, you need to build a table that displays all groups as columns and users as rows (or vice versa), and put a true/false in corresponding cell based if user belongs to group/parent group. At first, you need to get data.

Lets assume that you want to have users as columns, and public groups as rows. Then you need to store all group hierarchy information in next structure - Map>, where first id is Group Id, and second Id - is user id.

How to get those data -- at first, get all groups by querying Group object then you need to query users that you need to display -- do a SOQL on User object. Information about what belongs to the group is stored in GroupMember object, that have fields Groupid and UserOrGroupId. So, you query all the GroupMember objects, and then go through those records and fill your main map. That part of code may be tricky, because groups may have as children another groups, so you need carefully handle that case.

Map<Id,List<Id>> users_per_group = new Map<Id,List<Id>>();
List<GroupMember> all_members = [...];
for(GroupMember single_member : all_members){
    if (single_member.UserOrGroupId.getSobjectType()== Schema.User.SObjectType){
        if (users_per_group.get(single_member.GroupId) == null){
            users_per_group.get(single_member.GroupId) = new List<Id>();
        }
        users_per_group.get(single_member.GroupId).add(single_member.UserOrGroupId);
    }else{//child is group. 
          //so we need to add all child group users to current group
          //but we can do that later
    }

}

now we have in map users, that belongs to group directly. Lets build another structure, that have next view - Map. In this case, Id is group id, and Integer is amount of child groups, that belong to this group. If there are no child groups - there is no group record in map. code to populate that

Map<Id,Integer> child_groups_count = new Map<Id,Integer>();
for(GroupMember single_member : all_members){
    if (child_groups_count.get(single_member.groupId) == null){
        child_groups_count.get(single_member.groupId) = 0;
    }
    child_groups_count.get(single_member.groupId)++;
}

After that, we need to go through group members, and for every group, that have only child groups with users - add those child users to current group, and remove that group from second map. Do that until second map became empty. Here is code:

while(child_groups_count.keySet().size() > 0){
    for (Integer i = 0; i < all_members.size(); i++) 
        single_member = all_members.get(i);
        if(single_member.UserOrGroupId.getSobjectType()== Schema.Group.SObjectType){
            if (child_groups_count.get(single_member.UserOrGroupId) == null){
            //that mean that we have group with only users
                if (users_per_group.get(single_member.GroupId) == null){
                    users_per_group.get(single_member.GroupId) = new Set<Id>();
                }
                users_per_group.get(single_member.GroupId).addAll( users_per_group.get(single_member.UserOrGroupId));
                child_groups_count.get(single_member.GroupId)--;
                if (child_groups_count.get(single_member.GroupId) == 0){
                    child_groups_count.remove(single_member.GroupId);
                }
                //remove group member -- we already expanded main map
                all_members.remove(i);
                i--;
            }
        }
    }
} 

As a result -- we have map of groups, and users that belongs to that groups. Main idea is to use visualforce with:

<apex:repeat var="userId" value="<userids>" >

tag for list of all users, and inside that use one more for groups.

<apex:repeat var="userId" value="<userids>" >
<apex:repeat var="groupId" value="<groupIds>">
    <!-- display table cell here -->
</apex:repeat>
</apex:repeat

Based on evaluation of next criteria users_per_group.get(GroupId).contains(UserId) you can easily display true/false/another text etc.

I think that's enough information to build functionality that you need. Please, note - all code above is pseudocode,used to explain prototype and hasn't been tested.

Some useful links here: https://developer.salesforce.com/forums/?id=906F00000008yBGIAY

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_group.htm

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_groupmember.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_id.htm#apex_System_Id_getSObjectType

Best way to remove element from list or why they kept iterator without this option

0