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
Alex KirbyAlex Kirby 

Use variable in Apex for Dynamic list view

Hi All,

I would like to pass a variable to myu apex class to filter the list view returned:

I would like to add a field called category on to the visualforce page and have the SOQL where clause filter by the users selection i.e. if they select 'billing' I would want the query to be Select id, CaseNumber, ContactId, Subject, Status, Priority, CreatedDate from Case where category__c = billing'

Help is always appreciated, thanks

Class:
 
public with sharing class Pagination {


    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}    
    
    public ApexPages.StandardSetController setCon {
        get{
            if(setCon == null){
                size = 20;
                string queryString = 'Select id, CaseNumber, ContactId, Subject, Status, Priority, CreatedDate from Case';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
        }set;  
    }


    Public List<Case> getCases(){
        List<Case> caseList = new List<Case>();
        for(Case C : (List<Case>)setCon.getRecords())
            caseList.add(C);
        return caseList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getCases();
        setCon.setPageNumber(1);
        return null;
    }
     
    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }
  
    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }
  
    public void first() {
        setCon.first();
    }
  
    public void last() {
        setCon.last();
    }
  
    public void previous() {
        setCon.previous();
    }
  
    public void next() {
        setCon.next();
    }
}
VF:
<apex:page controller="Pagination" showHeader="false">
        <apex:pageBlock id="pb" mode="maindetail">
            <apex:pageBlockTable value="{!Cases}" var="a" >
                <apex:column headerValue="Case Number">
                <apex:outputlink value="/qmt_CaseDetail?id={!a.id}">{!a.CaseNumber}</apex:outputlink>
                </apex:column>
                <apex:column value="{!a.ContactId}"/>
                <apex:column value="{!a.Subject}"/>
                <apex:column value="{!a.Status}"/>
                <apex:column value="{!a.Priority}"/>
                <apex:column value="{!a.CreatedDate}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="|<" action="{!first}" disabled="{!!hasPrevious}" title="First Page"/>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="<" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page"/>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page"/>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page"/>
                <apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold;" >
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Best Answer chosen by Alex Kirby
Alex KirbyAlex Kirby
Managed to resolve this:
 
public with sharing class Pagination {


    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}    
    public string inputText1{get;set;}
    
    public ApexPages.StandardSetController setCon {
    
        get{
            if(setCon == null){
            
                size = 20;
                string queryString = 'Select id, CaseNumber, ContactId, Subject, Status, Priority, CreatedDate from Case where category__c=:inputText1';
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
           
        }set;  
    }
 
   
    
    Public List<Case> getCases(){
        List<Case> caseList = new List<Case>();
        for(Case C : (List<Case>)setCon.getRecords())
            caseList.add(C);
        return caseList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getCases();
        setCon.setPageNumber(1);
        return null;
    }
     
    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }
  
    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }
  
    public void first() {
        setCon.first();
    }
  
    public void last() {
        setCon.last();
    }
  
    public void previous() {
        setCon.previous();
    }
  
    public void next() {
        setCon.next();
    }
}
 
<apex:page controller="Pagination" showHeader="false">

<style>
    .metro {
  display: inline-block !important;
  padding: 5px !important;
  margin: 5px !important;
  background: #08C !important;
  background-position: 0 0 !important;
  /* Font styles */
  color: white !important;
  //font-weight: bold !important;
  text-decoration: none !important;
  text-align:center !important;
}

.metro:hover { background: #0AF; }

.metro.three-d {
  position: relative !important;
  box-shadow: 
    1px 1px #53A7EA,
    2px 2px #53A7EA,
    3px 3px #53A7EA !important;
  transition: all 0.1s ease-in !important;
}

.metro.three-d:active { 
  box-shadow: none !important;
  top: 2px !important;
  left: 2px !important;
}
</style>
    <apex:form >
   
   <apex:selectList value="{!InputText1}" multiselect="false" size="1">
                <apex:selectOption itemValue="Billing" itemLabel="Billing"/>
                <apex:selectOption itemValue="Metering" itemLabel="Metering"/>
                
   </apex:selectList>
   
   <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="Search" action="{!refresh}" title="Refresh Page"/>
            
        <apex:pageBlock id="pb" mode="maindetail">
            <apex:pageBlockTable value="{!Cases}" var="a" >
                <apex:column headerValue="Case Number">
                <apex:outputlink value="/qmt_CaseDetail?id={!a.id}">{!a.CaseNumber}</apex:outputlink>
                </apex:column>
                <apex:column value="{!a.ContactId}"/>
                <apex:column value="{!a.Subject}"/>
                <apex:column value="{!a.Status}"/>
                <apex:column value="{!a.Priority}"/>
                <apex:column value="{!a.CreatedDate}"/>
            </apex:pageBlockTable>
            <apex:panelGrid columns="7">
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="|<" action="{!first}" disabled="{!!hasPrevious}" title="First Page"/>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="<" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page"/>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value=">" action="{!next}" disabled="{!!hasNext}" title="Next Page"/>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value=">|" action="{!last}" disabled="{!!hasNext}" title="Last Page"/>
                <apex:outputText >{!(pageNumber * size)+1-size}-{!IF((pageNumber * size)>noOfRecords, noOfRecords,(pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton styleClass="metro" status="fetchStatus" reRender="pb" value="Refresh" action="{!refresh}" title="Refresh Page"/>
                <apex:outputPanel style="color:#4AA02C;font-weight:bold;" >
                    <apex:actionStatus id="fetchStatus" startText="Fetching..." stopText=""/>
                </apex:outputPanel>
            </apex:panelGrid>
        </apex:pageBlock>
    </apex:form>
</apex:page>