+ Start a Discussion
ChichoChicho 

Lookup - Picklist field to filter data in a Visualforce page

Can I filter the data displayed in a HTML table in a visualforce page by using a lookup field or a picklist field? My idea is to embedded a lookup field or a picklist field in the Visualforce page so the data displayed would be filter by the value previously selected.

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Yes you can filter records based on a picklist or a lookup field. Your code would be something like

 

<apex:page controller="MyFilter">
<apex:form>
<apex:inputField value="{!YourObjReference.LookupField__c}"/> <apex:pageblock> <apex:commandButton action="{!filter}" value="FILTER"> <apex:pageblockTable var="{!yourObjList}" var="obj"> <apex:column value="{!obj.name}"/> </apex:pageblockTable> </apex:pageblock>
</apex:form> </apex:page>

 Controller

 

public class MyFilter{
  public MyFilter(){
    YourObjReference = new CustomObject__c();

  }
  public CustomObject__c YourObjReference{get;set;}
  public List<CustomObject__c>  yourObjList{get;set;}
  public void filterRecords(){
    yourObjList = new List<CustomObject__c>();
    yourObjList = [SELECT Name FROM ]CustomObject__c WHERE LookupField__c=:YourObjReference.LookupField__c];
  }

}

 Well replace "CustomObject__c" , "LookupField__c"  with proper api name of your object and this code should work fine(can have syntax errors :P )

All Answers

Avidev9Avidev9

Yes you can filter records based on a picklist or a lookup field. Your code would be something like

 

<apex:page controller="MyFilter">
<apex:form>
<apex:inputField value="{!YourObjReference.LookupField__c}"/> <apex:pageblock> <apex:commandButton action="{!filter}" value="FILTER"> <apex:pageblockTable var="{!yourObjList}" var="obj"> <apex:column value="{!obj.name}"/> </apex:pageblockTable> </apex:pageblock>
</apex:form> </apex:page>

 Controller

 

public class MyFilter{
  public MyFilter(){
    YourObjReference = new CustomObject__c();

  }
  public CustomObject__c YourObjReference{get;set;}
  public List<CustomObject__c>  yourObjList{get;set;}
  public void filterRecords(){
    yourObjList = new List<CustomObject__c>();
    yourObjList = [SELECT Name FROM ]CustomObject__c WHERE LookupField__c=:YourObjReference.LookupField__c];
  }

}

 Well replace "CustomObject__c" , "LookupField__c"  with proper api name of your object and this code should work fine(can have syntax errors :P )

This was selected as the best answer
ChichoChicho

Thanks a lot!! I wrote the code and worked :) I'm trying now to add a second filter which values would depend on those previoudly selected in the first filter. How can I do this? 

Ramesh VaratharajRamesh Varatharaj

Hi Avi, I tried your code and getting below error. could you please help me to fix it. Custom object name: Sales_S__C, picklist field: Stage__c

 

Error: Unknown method 'MyFilter.filter()'

 

VF Page:

<apex:page controller="MyFilter">
<apex:form>
<apex:inputField value="{!YourObjReference.Stage__c}"/>
<apex:pageblock>
  <apex:commandButton action="{!filter}" value="FILTER" />
  <apex:pageblockTable value="{!yourObjList}" var="obj">
    <apex:column value="{!obj.name}"/>
  </apex:pageblockTable>
</apex:pageblock>
</apex:form>
</apex:page>

 

Controller:

public class MyFilter{

  public MyFilter(){

    YourObjReference = new Sales_C__c();

  }

  public Sales_C__c YourObjReference{get;set;}

  public List<Sales_C__c>  yourObjList{get;set;}

  public void filterRecords(){

    yourObjList = new List<Sales_C__c>();

    yourObjList = [SELECT Name FROM Sales_C__c WHERE Stage__c=:YourObjReference.Stage__c];

  }

}

Ramesh VaratharajRamesh Varatharaj

I found the answer. Thanks anyways. It really helps me.

Sudhir_MeruSudhir_Meru
Hi Ramesh 

How did you fix Error: Unknown method 'MyFilter.filter()' ? 

 I am getting same error please suggest.

Thanks
Sudhir
Daniel Pfeiffer 6Daniel Pfeiffer 6
Hi Ramesh, 

could you please share the myfilter.filter solution with us as well? :) thanks
Will EtheridgeWill Etheridge
The solution to: "Unknown method 'MyFilter.filter()"

is to change Avidev9's Visualforce Page Line 4 from:

<apex:commandButton action="{!filter}" value="FILTER" />

to

<apex:commandButton action="{!filterRecords}" value="FILTER" />