0

How to display public group users,when i had given group name in search bar need to display the users to particular group name in visualforce page? enter image description here anybody help me.....

0

You can add the list of available groups using the below code change.

VF PAGE

<apex:selectList size="1">
    <apex:selectOptions value="{!Groups}"></apex:selectOptions>
</apex:selectList>

CONTROLLER

public List<SelectOption> getGroups(){
    List<SelectOption> ansprojoptions = new List<SelectOption>();    
    for(Group g: [select id ,name from group]){
        if(g.name!=null){
            ansprojoptions.add(new SelectOption(g.name,g.name));
        }
    }       
    return ansprojoptions ;
}

Use the value of {!Groups} in your code if you want to implement search from this.

Output

1

I have built a Vf Page and custom controller to display the username using a wrapper which holds UserName and the GroupMember details if more userInformation is required you can modify the wrapper and the User SOQL query to fetch and map it accordingly in the wrapper.

VF PAGE

<apex:page controller="fetchUsers">
    <apex:form >
        <apex:inputText value="{!searchText}"/>
        <apex:commandButton value="Search Results" action="{!findUsers}" reRender="pb1"/>
        <apex:pageBlock id="pb1">
            <apex:pageBlockTable value="{!listUserGroupMemberWrapper}" var="lg" id="pbt1">
                <apex:column headerValue="Group" value="{!lg.grpMember.Group.name}"/>
                <apex:column headerValue="User" value="{!lg.UserName}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

CONTROLLER

public with sharing class fetchUsers {

Public String searchText {get;set;}


public list<UserGroupMemberWrapper> listUserGroupMemberWrapper {get;set;}

public list<GroupMember> listGroupMember = new list<GroupMember>();
private list<Group> listGroup = new list<Group>();
private Map<Id,User> MapUser;
Private set<ID> groupIds = new set<ID>();
Private set<ID> UserIds= new set<ID>();

    public PageReference findUsers() {
        String SrcText='%'+searchText+'%';
        For(Group g : [select id from group where name like :SrcText]){
            groupIds.add(g.id);
        }
        listGroupMember = [Select id, Group.name,groupId,UserOrGroupId from GroupMember where groupId IN:groupIds]; 
        for(GroupMember gm : listGroupMember ){
            if(String.valueof(gm.UserOrGroupId).contains('005')){
                UserIds.add(gm.UserOrGroupId);
            }
        }
        MapUser = new Map<Id,User>([select id,name from user where Id IN:UserIds]);
        listUserGroupMemberWrapper = new List<UserGroupMemberWrapper>();
        for(GroupMember gm : listGroupMember ){
            if(String.valueof(gm.UserOrGroupId).contains('005')){
                UserGroupMemberWrapper ugmWrap = new UserGroupMemberWrapper();
                ugmWrap.UserName=MapUser.get(gm.UserOrGroupId).name;
                ugmWrap.grpMember=gm;
                listUserGroupMemberWrapper.add(ugmWrap);
            }
        }

        return null;

    }

    public class UserGroupMemberWrapper{
        public String UserName{get;set;}
        public GroupMember grpMember{get;set;}

        private UserGroupMemberWrapper(){
            grpMember = new GroupMember();
        }

    }
}

Output

  • Welcome! Let me know if you need any other help! Please mark as a best answer! – Nishant Singh Panwar Oct 23 '15 at 9:34
  • Nishant i need one more help.how to display the public groups in dropdown list in page.can you help me... – user Oct 23 '15 at 9:43
  • Instead oh the search text? You can have a picklist and populate the select options using the SOQL query on Group Object in the constructor of controller. – Nishant Singh Panwar Oct 23 '15 at 9:51

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.