1

Display Public Groups in drop down list & in this list select one public group, then press search button, on pressing search button display the all users in particular public group.

2

To display Public groups in drop down list and in Available Public Groups list select one public group,then press List Users button. After pressing the List Users button, you will get the all Available Users associated with that groups as shown in below screen shot as well.

Output:
enter image description here

Code:

Visualforce Page Code

<apex:page controller="GroupsController" >
    <br/>
    <div style="padding:30px;">
        <apex:form >
            <div style="text-align:center;">
                <b style="padding-bottom:15px;">Available Public Groups</b>
                <br/>
                <apex:selectList value="{!selectedVal}" size="10" style="width:305px;">
                    <apex:selectOptions value="{!groupsList}"/>
                </apex:selectList>
            </div>
            <br/>
            <center><apex:commandButton action="{!setmyValue}" reRender="textDisplay" value="List Users"/></center>
            <br/>
            <div style="text-align:center;">
                <b style="padding-bottom:15px;">Available Users</b>
                <br/>
                <apex:outputPanel id="textDisplay">
                    <apex:selectList value="{!selectedVal1}"  size="10" style="width:305px;">
                        <apex:selectOptions value="{!users_id}"/>
                    </apex:selectList>
                </apex:outputPanel>
            </div>
        </apex:form>
    </div>  
</apex:page>

Apex Code

public class GroupsController {
    public String selectedVal{
        get{return selectedVal;}set{selectedVal= value;}
    }
    public String selectedVal1{
    get;set;
    }
    public List<SelectOption> users_id {
         get{return users_id;}
         set{users_id=value;}
    }
    public PageReference setmyValue(){
         List<GroupMember> gm = [SELECT userorgroupid from GroupMember where groupid=:selectedVal];  
         List<String> group_id  = new  List<String>();
         for(GroupMember g:gm){
              group_id.add(g.userorgroupid+'');
         }
         List<USER> users_1  = [SELECT name from User where id in :group_id];
         users_id = new List<SelectOption>();
          for(User u:users_1 ){
              users_id.add(new SelectOption(u.name,u.name));
         }
        return null;
    }
    public List<selectOption> getGroupsList(){        
            SET<String> fields = new SET<String>();
                        fields = Schema.SObjectType.Group.fields.getmap().keyset();
                        System.debug(fields);
                        List<Group> groups = [select developername,name, type from group where type= 'Regular'];
                        Map<id,Group> group_maps = new Map<id,Group>();
                        List<selectOption> g1roupsList = new List<selectOption>();
            for(Group g :groups){
                        group_maps.put(g.id,g);
                g1roupsList.add(new SelectOption(g.id,g.developername));
                        }
                        System.debug(group_maps);
            return g1roupsList;
    }
}

Try the code and do practice. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.