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
udayar_jayamudayar_jayam 

How to populate users in picklist based on their Profile?

Hi All,

    How to populate users based on their profile?

For e.g. I have taken lookup field which consist of all the profile.

Once a role is selected, a picklist should get populate with all the users under that category of profile.

HariniHarini

Your question is not clear. Are you trying to ppoulate user with a particluar profile or a particular role. I have provide the queries for both

 

VF PAge : (Profile Selection)

 

<apex:selectList id="selectList" size="1" multiselect="false" value="{!ProfileSelected}" >                    
                 <apex:selectOptions value="{!PopulateUsers}"/>  
  </apex:selectList>

 

In the Controller:

 

public String ProfileSelected{get; set;}


public List<SelectOption> getPopulateUsers() {
  List<SelectOption> options = new List<SelectOption>();

     for(User usr:[Select  Name,Id From User  where isactive = true and ProfileId=:ProfileSelected]) {
    options.add(new SelectOption(usr.id,usr.name));
  }
  return options;

 

VF PAge : (Role Selection)

 

<apex:selectList id="selectList" size="1" multiselect="false" value="{!RoleSelected}" >                    
                 <apex:selectOptions value="{!PopulateUsers}"/>  
  </apex:selectList>

 

In the Controller:

 

public String RoleSelected{get; set;}


public List<SelectOption> getPopulateUsers() {
  List<SelectOption> options = new List<SelectOption>();

//Select u.UserRoleId, u.ProfileId, u.Name, u.Id From User u where isactive = true and ProfileId=:ProfileSelected;
     for(User usr:[Select  Name,Id From User  where isactive = true and UserRoleId=:RoleSelected]) {
    options.add(new SelectOption(usr.id,usr.name));
  }
  return options;

 

 

Let me know if this is what you are looking for.

Yoganand GadekarYoganand Gadekar

You can try following VF page and controller,

 

-- Visual Force Page --

 

<apex:page standardController="Account" extensions="populateUser">
 <apex:form >
    <apex:pageBlock >
       
        <apex:inputField label="Profile" value="{!acc.owner.profileid}" required="false"/>
        <apex:inputField label="Role" value="{!acc.owner.userroleid}"/>&nbsp;&nbsp;&nbsp;&nbsp;
        <apex:commandButton value="Show Users" action="{!Showusers}"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       
        <apex:selectList id="selectList" size="1" multiselect="false" value="{!UserSelected}" >                   
            <apex:selectOptions value="{!PopulateUsers}"/> 
        </apex:selectList>
   
    </apex:pageBlock>
 </apex:form>
</apex:page>

 

-- Controller --

 

public class populateUser {
Public string UserSelected{get;set;}
Public Account acc{get;set;}
Public List<User> UserList {get;set;}
Public Boolean showuserList{get;set;}  
   
  public populateUser(ApexPages.StandardController controller) {
        UserList = new List<user>();
        showuserList = false;
        user us = new user(profileid =null);
        acc = new account(owner= us
        );
  }

  Public Void Showusers(){
      showuserList = true;
      if(acc.owner.profileid != null && acc.owner.userRoleId !=null)
         UserList = [select id,name from user where profileid =:acc.owner.profileid and UserRoleID=:acc.owner.userroleid];
      else if(acc.owner.profileid != null && acc.owner.userRoleId ==null) 
              UserList = [select id,name from user where profileid =:acc.owner.profileid ];
  }

  public List<SelectOption> getPopulateUsers() {
       List<SelectOption> options = new List<SelectOption>();
       if(UserList.size()==0)
          options.add(new SelectOption(' --None-- ',' --None-- '));
  
       if(acc.owner.profileid != null){
          for(User usrList :UserList ){
              options.add(new SelectOption(usrList.name,usrList.name));
          }
     }
     return options;
  }
}

 

Mark this as answer and hit kudos if it helps you.

 

Thanks,

Yoganand.