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.


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.

enter image description here


Visualforce Page Code

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

Apex Code

public class GroupsController {
    public String selectedVal{
        get{return selectedVal;}set{selectedVal= value;}
    public String selectedVal1{
    public List<SelectOption> users_id {
         get{return users_id;}
    public PageReference setmyValue(){
         List<GroupMember> gm = [SELECT userorgroupid from GroupMember where groupid=:selectedVal];  
         List<String> group_id  = new  List<String>();
         for(GroupMember g:gm){
         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();
                        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){
                g1roupsList.add(new SelectOption(g.id,g.developername));
            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.