+ Start a Discussion
Prasanthi BPrasanthi B 

Passing picklist value to a controller query

I need to pass a selected picklist value to my controller class and based on that value, a query needs to be created. Then the resulting values have to be displayed in a table.

 

Please find my VF and Controller code and suggest.

 

Thankyou.

 

VF CODE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
   <apex:selectList id="u" value="{!usr}" size="1" title="Users">
   <apex:selectOptions value="{!usrs}"></apex:selectOptions>
   </apex:selectList>
   <apex:pageBlockButtons >  
<apex:commandButton value="GO" action="{!doSearch}">            
    </apex:commandButton>
    </apex:pageBlockButtons>
    <apex:pageBlockTable value="{!Results}" var="names">
    </apex:pageBlockTable>
   <br></br>             
  
  </apex:pageblock>
  </apex:form>   
  </apex:page>

 

 

 

CONTROLLER:

 

public class UserData {
PUBLIC String usr{get;set;}
PUBLIC List<selectOption> usrs;
List<User> results;
List<Id> userlist=new List<Id>();

   public UserData(ApexPages.StandardController controller)
   { 
    }
  
   public List<selectOption> getusrs()
  {
    List<selectOption> options = new List<selectOption>(); 
    options.add(new selectOption('', '- None -'));   
    FOR (User users : [SELECT Id,Region__c FROM User Order By Region__c Asc]) 
    {   
      options.add(new selectOption(users.Id, users.Region__c));
     }    
       return options; 
  } 
  
 
   public Pagereference doSearch()
     {

      //selected picklist value must be passed to this query.
       results = (List<User>)[select Name from User Where Id=:???????????];
       system.debug('USERSLISTFOR REGION$$$$'+results);
       return null;
     }
      
   public List<User> getResults()
    {
        return results;
    }
 
 }

Best Answer chosen by Admin (Salesforce Developers) 
APathakAPathak
Hi,
Please see the changes in bold.

 

VFCODE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
    
   <apex:inputField id="startMode" value="{!User.Region__c}" />
   <br></br>
  
   <apex:commandButton action="{!doSearch}" value="Search">               
   </apex:commandButton>
 
  <br></br>             
 
  </apex:pageblock>
  </apex:form>
  </apex:page>

 

 

CONTROLLER:

 

public class UserData {

private User user;
PUBLIC String usr{get;set;}
List<User> results=new List<User>();
List<Id> userlist=new List<Id>();
String Regionid;
String Region;

   public UserData(ApexPages.StandardController controller)
   {
            this.user= (User)controller.getRecord();

   }
  
  
  public Pagereference doSearch()
     {
      results = [select Name from User Where Region__c = :User.Region__c];     
      system.debug('USERSLISTFOR REGION$$$$'+results);
      return null;
     }
      
   public List<User> getResults()
    {
        return results;
    }
 
 }

 


 

All Answers

APathakAPathak

The str value will get passed on to controller on pressing "GO" button through the setter method. As for query,hope this is what you are looking for :-

      

results = (List<User>)[select Name from User Where Region__c  = :str];

 

Prasanthi BPrasanthi B

 

 

 

 

Hi,

 

Thnakyou for your reply.

 

The 'str' value is assigned no where in the controller class. How the selected picklist value would be passed to Str??

 

 

JHayes SDJHayes SD

The controller variable that you specify in the value attribute of the <apex:selectList> element should contain the currently selected picklist value.  Looks like in your code this is mapped to {!usr}.  Try this in your query:

 

String queryString = 'Select Name from User Where Id=\'' + usr + '\'';
List<User> results = Database.Query(queryString);

 

APathakAPathak

Hi,

Sorry for the typo.I suppose that you are looking for the users of the region selected from the picklist. A map can be used here to store the Region of the users and subsequently the region  can be retrieved for the selected user in picklist. See my changes in bold.

 


TESTOURSFDC wrote:

I need to pass a selected picklist value to my controller class and based on that value, a query needs to be created. Then the resulting values have to be displayed in a table.

 

Please find my VF and Controller code and suggest.

 

Thankyou.

 

VF CODE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
   <apex:selectList id="u" value="{!usr}" size="1" title="Users">
   <apex:selectOptions value="{!usrs}"></apex:selectOptions>
   </apex:selectList>
   <apex:pageBlockButtons >  
<apex:commandButton value="GO" action="{!doSearch}">            
    </apex:commandButton>
    </apex:pageBlockButtons>
    <apex:pageBlockTable value="{!Results}" var="names">
    </apex:pageBlockTable>
   <br></br>             
  
  </apex:pageblock>
  </apex:form>   
  </apex:page>

 

 

 

CONTROLLER:

 

public class UserData {
PUBLIC String usr{get;set;}
PUBLIC List<selectOption> usrs;

map <String,String> mapUsers = new map <id,String>();
List<User> results;
List<Id> userlist=new List<Id>();

   public UserData(ApexPages.StandardController controller)
   { 
    }
  
   public List<selectOption> getusrs()
  {
    List<selectOption> options = new List<selectOption>(); 
    options.add(new selectOption('', '- None -'));   
    FOR (User users : [SELECT Id,Region__c FROM User Order By Region__c Asc]) 
    {   
      options.add(new selectOption(users.Id, users.Region__c));

      mapUsers.put(users.Id, users.Region__c);
     }    
       return options; 
  } 
  
 
   public Pagereference doSearch()
     {

      //selected picklist value must be passed to this query.
       results = (List<User>)[select Name from User Where Region__c = :mapUsers.get(usr)];
       system.debug('USERSLISTFOR REGION$$$$'+results);
       return null;
     }
      
   public List<User> getResults()
    {
        return results;
    }
 
 }



)];

Prasanthi BPrasanthi B

Hi,

 

Thankyou for your update.

 

But, please find my updated code as below. I have removed the select list option tag and using input field tag. Region is a custom picklist field in User Object.

 

Please suggest in this case.

 

VFCODE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
    
   <apex:inputField id="startMode" value="{!User.Region__c}" />
   <br></br>
  
   <apex:commandButton action="{!doSearch}" value="Search">               
   </apex:commandButton>
 
  <br></br>             
 
  </apex:pageblock>
  </apex:form>
  </apex:page>

 

 

CONTROLLER:

 

public class UserData {
PUBLIC String usr{get;set;}
List<User> results=new List<User>();
List<Id> userlist=new List<Id>();
String Regionid;
String Region;

   public UserData(ApexPages.StandardController controller)
   {
   }
  
  
  public Pagereference doSearch()
     {
      results = [select Name from User Where Region__c = ?????????];     
      system.debug('USERSLISTFOR REGION$$$$'+results);
      return null;
     }
      
   public List<User> getResults()
    {
        return results;
    }
 
 }

 

APathakAPathak
Hi,
Please see the changes in bold.

 

VFCODE:

 

<apex:page StandardController="User" id="thePage" Extensions="UserData">
  <apex:form >
  <apex:pageBlock title="SelectUsers" id="USERSLIST" mode="edit">
    <h1>Update Sales Co-ordinator for the users in Particular Region</h1>
  <br></br>
  <br></br>
   Select Region of the user:
    
   <apex:inputField id="startMode" value="{!User.Region__c}" />
   <br></br>
  
   <apex:commandButton action="{!doSearch}" value="Search">               
   </apex:commandButton>
 
  <br></br>             
 
  </apex:pageblock>
  </apex:form>
  </apex:page>

 

 

CONTROLLER:

 

public class UserData {

private User user;
PUBLIC String usr{get;set;}
List<User> results=new List<User>();
List<Id> userlist=new List<Id>();
String Regionid;
String Region;

   public UserData(ApexPages.StandardController controller)
   {
            this.user= (User)controller.getRecord();

   }
  
  
  public Pagereference doSearch()
     {
      results = [select Name from User Where Region__c = :User.Region__c];     
      system.debug('USERSLISTFOR REGION$$$$'+results);
      return null;
     }
      
   public List<User> getResults()
    {
        return results;
    }
 
 }

 


 

This was selected as the best answer