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
louisa barrett 7louisa barrett 7 

Populate SelectList from function populated by SOQL query

I'm trying to create a filter on a pageBlock and am running into difficulties.
This is my code currently:

Apex:
public with sharing class ChildOpportunitiesAP {
    private id Parent_Acc_Id;
    Public Integer noOfRecords{Get;Set;}
    Public Integer size {get;Set;}
    
          public ApexPages.StandardSetController controller {
                    get{
                 Parent_Acc_Id = ApexPages.currentPage().getparameters().get('id');
                        if(controller == null){
                            size = 4;
                            string queryString = 'SELECT Account.Name, Name, Total_Opportunity_Amount__c ' +
                                ' FROM Opportunity WHERE Account.Parent.id=: Parent_Acc_Id';
                                 controller = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                        controller.setPageSize(size);
                            noOfRecords = controller.getResultSize();
             }
             return controller;                              
            }set;
        }
    
         public List<Opportunity> getRelatedOpportunities(){
                        List <Opportunity> childOpps = New List<Opportunity>();
        FOR(Opportunity opp:(List<Opportunity>)controller.getRecords())
            childOpps.add(opp);
                 Return childOpps;   
         }
    
        public pageReference refresh(){
            controller = null;
            getRelatedOpportunities();
            controller.setPageNumber(1);
            return null;
        }
    
    public list<selectOption> getAvailableFilters(){
       List<selectOption> options = new List<SelectOption>();
        for(Opportunity opp: getRelatedOpportunities)
        {
            options.add(new SelectOption(opp.Name,opp.Name));
        }
       return options;
    }
 }

VF:

<apex:page sidebar="false" showHeader="false" Controller="ChildOpportunitiesAP">
   <apex:form >
          <apex:pageBlock title="Linked child opportunities" id = "pb">
              Filter:
              <apex:selectList> value="{! availableFilters }" size="1">
                <apex:selectOptions value="{! Account.Name  }"/>
                <apex:actionSupport event="onchange" reRender="pb"/>
              </apex:selectList>
          <apex:pageBlockTable value="{! RelatedOpportunities}" var="opp">
              <apex:column value="{! opp.Account.Name}"/>
              <apex:column value="{! opp.Name}"/>
              <apex:column value="{! opp.Total_Opportunity_Amount__c}"/>
          </apex:pageBlockTable>
        <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!controller.first}" disabled="{!!controller.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!controller.previous}" disabled="{!!controller.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!controller.next}" disabled="{!!controller.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!controller.last}" disabled="{!!controller.hasNext}" title="Last Page"/>
                <apex:outputText >{!(controller.pageNumber * size)+1-size}-{!IF((controller.pageNumber * size)>noOfRecords, noOfRecords,(controller.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton 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>

I would like the user to be able to click on the ppp.Account.Name column and see a list of populated account names.
If it's possible I'd like the user to be able to filter by the ppp.Account.Name and the opp.Name columns; I'm not sure if that is possible or not.

Many thanks for any help
 
Best Answer chosen by louisa barrett 7
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Lousia,
Replace your selectlist with this one -
<apex:selectList value="{! selectListValue }" size="1">
     <apex:selectOptions value="{! availableFilters   }"/>
     <apex:actionSupport event="onchange" reRender="pb"/>
</apex:selectList>

"selectListValue " this will hold the select list value selected by the user.
In the apex class , define this variable  as - 
public string selectListValue {get;Set;}

Hope it will help yoiu.

Thanks,
Sumit Kumar Singh

All Answers

Sumit Kumar Singh 9Sumit Kumar Singh 9
Hello Lousia,
Replace your selectlist with this one -
<apex:selectList value="{! selectListValue }" size="1">
     <apex:selectOptions value="{! availableFilters   }"/>
     <apex:actionSupport event="onchange" reRender="pb"/>
</apex:selectList>

"selectListValue " this will hold the select list value selected by the user.
In the apex class , define this variable  as - 
public string selectListValue {get;Set;}

Hope it will help yoiu.

Thanks,
Sumit Kumar Singh
This was selected as the best answer
louisa barrett 7louisa barrett 7
Thank you very much. I'll try it first thing tomorrow Sent from Samsung Mobile on O2
louisa barrett 7louisa barrett 7
Morning,
I've declared the variable, but I don't understand how that ties to my getAvailableFilters method which I thought I needed to pull the available filters e.g. Opportunity names etc.
I've copied the code from the controller and the VF page along with the two errors I'm getting.

Apex code:
public with sharing class ChildOpportunitiesAP {
    private id Parent_Acc_Id;
    Public Integer noOfRecords{Get;Set;}
    Public Integer size {get;Set;}
    public String selectListValue{get;set;}
    
          public ApexPages.StandardSetController controller {
                    get{
                 Parent_Acc_Id = ApexPages.currentPage().getparameters().get('id');
                        if(controller == null){
                            size = 4;
                            string queryString = 'SELECT Account.Name, Name, Total_Opportunity_Amount__c ' +
                                ' FROM Opportunity WHERE Account.Parent.id=: Parent_Acc_Id';
                                 controller = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                        controller.setPageSize(size);
                            noOfRecords = controller.getResultSize();
             }
             return controller;                              
            }set;
        }
    
         public List<Opportunity> getRelatedOpportunities(){
                        List <Opportunity> childOpps = New List<Opportunity>();
        FOR(Opportunity opp:(List<Opportunity>)controller.getRecords())
            childOpps.add(opp);
                 Return childOpps;   
         }
    
        public pageReference refresh(){
            controller = null;
            getRelatedOpportunities();
            controller.setPageNumber(1);
            return null;
        }  
    
    public list<selectOption> getAvailableFilters(){
       List<selectOption> options = new List<SelectOption>();
        for(Opportunity opp: RelatedOpportunities)
        {
            options.add(new SelectOption(opp.Name,opp.Name));
        }
       return options;
    }
    }

VF Page:
 
<apex:page sidebar="false" showHeader="false" Controller="ChildOpportunitiesAP">
   <apex:form >
          <apex:pageBlock title="Linked child opportunities" id = "pb">
              Filter:
              <apex:selectList> value="{! selectListValue }" size="1">
                <apex:selectOptions value="{! AvailableFilters  }"/>
                <apex:actionSupport event="onchange" reRender="pb"/>
              </apex:selectList>
          <apex:pageBlockTable value="{! RelatedOpportunities}" var="opp">
              <apex:column value="{! opp.Account.Name}"/>
              <apex:column value="{! opp.Name}"/>
              <apex:column value="{! opp.Total_Opportunity_Amount__c}"/>
          </apex:pageBlockTable>
        <apex:panelGrid columns="7">
                <apex:commandButton status="fetchStatus" reRender="pb" value="|<" action="{!controller.first}" disabled="{!!controller.hasPrevious}" title="First Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value="<" action="{!controller.previous}" disabled="{!!controller.hasPrevious}" title="Previous Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">" action="{!controller.next}" disabled="{!!controller.hasNext}" title="Next Page"/>
                <apex:commandButton status="fetchStatus" reRender="pb" value=">|" action="{!controller.last}" disabled="{!!controller.hasNext}" title="Last Page"/>
                <apex:outputText >{!(controller.pageNumber * size)+1-size}-{!IF((controller.pageNumber * size)>noOfRecords, noOfRecords,(controller.pageNumber * size))} of {!noOfRecords}</apex:outputText>
                <apex:commandButton 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>


Errors:
User-added image