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
sai harishsai harish 

Need help passing List<selectoption> in to my query.

This might be simple but I am unable to figure out what I am missing. I need to get Project Managers from the VF page and pass them in to my Query. Here is my Apex class and my VF page.
Class :
public class ProjectsFilterByDates{
public ProjectsFilterByDates(ApexPages.StandardController stdController) {
  }
  Date StartDate;
  Date EndDate;
  public String DateStringProperty{get;set;}  
  List<AcctSeed__Project__c> results;
  String[] ProjectManagers = new String[]{};
  public Date getstartDate(){
  return StartDate;
  }
  public Date getEndDate(){
  return EndDate;
  }
  Public List<AcctSeed__Project__c> getresults(){
  return results;
  }
  public void setStartDate(Date input){
  StartDate = input;
  }
  public void setEndDate(Date input1){
  EndDate = input1;
  }
  
     public List<SelectOption> getListOfUser(){
     List<User> Users = [select Name from user Where Project_Manager__c = true Order By Name ASC] ;
     List<SelectOption> options = new List<SelectOption>();
        for(User u : Users)
               {
                options.add(new SelectOption(u.Id , u.Name));
               }
                  return options;
                  }
  public String[] getProjectManagers (){
  return ProjectManagers;
  }
  public void setProjectManagers (string[] ProjectManagers){
  this.ProjectManagers = ProjectManagers;
  }   

  public PageReference Search(){
  results = [SELECT Name,UserID__c,AcctSeed__Account__r.Name,Date_Created__c,Construction_Manager__r.Name,AcctSeedERP__Manufactured_Product__r.Name,Project_Manager__c FROM AcctSeed__Project__c WHERE Date_Created__c >= :StartDate AND Date_Created__c <= :EndDate AND Project_Manager__c =:ProjectManagers order by Date_Created__c desc ];
  return null;
  }
}
VF Page:
<apex:page Standardcontroller="AcctSeed__Project__c" extensions="ProjectsFilterByDates" showHeader="false" sidebar="false" docType="html-5.0" language="en_Us" >
<apex:form >
<apex:pageBlock >
 Start Date: <apex:input type="date" value="{!StartDate}"/>
 End Date: <apex:input type="date" value="{!EndDate}"/>
<apex:selectList value="{!ProjectManagers}" size="5" multiselect="true">
<apex:selectOptions value="{!ListOfUser}"/>
</apex:selectList><p/>

<apex:commandButton value="Search For Projects" action="{!search}"/>
<apex:pageBlockTable value="{!results}" var="p">
<apex:column value="{!p.Name}"/>
<apex:column value="{!p.AcctSeed__Account__r.Name}"/>
<apex:column value="{!p.Project_Manager__c}"/>
<apex:column value="{!p.Date_Created__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Best Answer chosen by sai harish
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Optimize or Modify your controller class to below snippet
 
public class ProjectsFilterByDates{

  public ProjectsFilterByDates(ApexPages.StandardController stdController) { }
  

  public String DateStringProperty {get;set;}  
  public List<AcctSeed__Project__c> results {get;set;}
  public Date StartDate {get;set;}
  public Date EndDate {get;set;}
  public String ProjectManagers {get;set;}

 
  
  public List<SelectOption> getListOfUser(){

       List<User> Users = [select Name from user Where Project_Manager__c = true Order By Name ASC] ;
        List<SelectOption> options = new List<SelectOption>();
        for(User u : Users)
               {
                options.add(new SelectOption(u.Id , u.Name));
               }
        return options;
  }

 
  public PageReference Search(){

	  results = [SELECT Name,UserID__c,AcctSeed__Account__r.Name,Date_Created__c,Construction_Manager__r.Name,AcctSeedERP__Manufactured_Product__r.Name,Project_Manager__c FROM AcctSeed__Project__c WHERE Date_Created__c >= :StartDate AND Date_Created__c <= :EndDate AND Project_Manager__c =:ProjectManagers order by Date_Created__c desc ];

	  return null;

  }
  
}

 

All Answers

Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Optimize or Modify your controller class to below snippet
 
public class ProjectsFilterByDates{

  public ProjectsFilterByDates(ApexPages.StandardController stdController) { }
  

  public String DateStringProperty {get;set;}  
  public List<AcctSeed__Project__c> results {get;set;}
  public Date StartDate {get;set;}
  public Date EndDate {get;set;}
  public String ProjectManagers {get;set;}

 
  
  public List<SelectOption> getListOfUser(){

       List<User> Users = [select Name from user Where Project_Manager__c = true Order By Name ASC] ;
        List<SelectOption> options = new List<SelectOption>();
        for(User u : Users)
               {
                options.add(new SelectOption(u.Id , u.Name));
               }
        return options;
  }

 
  public PageReference Search(){

	  results = [SELECT Name,UserID__c,AcctSeed__Account__r.Name,Date_Created__c,Construction_Manager__r.Name,AcctSeedERP__Manufactured_Product__r.Name,Project_Manager__c FROM AcctSeed__Project__c WHERE Date_Created__c >= :StartDate AND Date_Created__c <= :EndDate AND Project_Manager__c =:ProjectManagers order by Date_Created__c desc ];

	  return null;

  }
  
}

 
This was selected as the best answer
sai harishsai harish
Hi Santosh. Thank you for your quick response. I tried to do it the way you suggested. But unfortunately it is not working for me. But when I hard code the user values, It is getting it from the visualforce page and I am getting the required Results. Dont Know what the problem is. Any more suggestions from you are appreciated.
Thanks in Advance.
Sai Harish.
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Try this in your vf page.
 
<apex:selectList value="{!ProjectManagers}" size="5" multiselect="true" rendered = "true">
<apex:selectOptions value="{!ListOfUser}"/>
</apex:selectList>

 
sai harishsai harish
Hi Santosh,
Thank you for the help.
Here is my final code which is working.

public class projectManagers{
public String DateStringProperty {get;set;}  
  public List<AcctSeed__Project__c> results {get;set;}
  public Date StartDate {get;set;}
  public Date EndDate {get;set;}
  String[] ProjectManagers = new String[]{};

   public List<SelectOption> getListOfUser(){
        List<SelectOption> options = new List<SelectOption>();
        for(User u : [Select Id, Name From User Where Project_Manager__c = true Order By Name])
               {
                options.add(new SelectOption(u.Name,u.Name));
               }
        return options;
  }
  public String[] getProjectManagers (){
  return ProjectManagers;
  }
  public void setProjectManagers (string[] ProjectManagers){
  this.ProjectManagers = ProjectManagers;
  }

   
  public PageReference Search(){

      results = [SELECT Id,Name,Project_Manager__c,AcctSeed__Status__c  from AcctSeed__Project__c Where AcctSeed__Status__c =:Status AND Project_Manager__c=:ProjectManagers LIMIT 1000];

      return null;
  }
}
I had to do the part which is in bold other wise Project Managers were not passing in my query.
Thank you so much for the help Santosh.