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
Jacky LeeJacky Lee 

Passing a Visualforce picklist value input to an APEX variable

Hi guys,

I have a Visualforce table that I'd like to dynamically filter through a VF picklist. The filter is a string, which is "Currency". So by default it is on USD and will show all records that are in USD, but I'd like to pick 'CAD' from a dropdown on the page and it will rerender the table with only CAD records.

I have borrowed some code from this discussion, but the button is so far unresponsive and doesn't rerender. I've attached my code below with only the relevant parts:

VISUALFORCE CODE
<apex:page controller="InvoicesReadyToPayV4">
    <apex:form>
    	<apex:pageBlock title="Test Report">
            <apex:pageBlockSection columns="1">
            	Select Currency:
                <apex:selectList size="1" value="{!currencyPickvalue}">
                	<apex:commandButton value="Go" action="{!getCurrency}" reRender="table1"/>
                	<apex:selectOption itemLabel="USD" itemValue="USD"></apex:selectOption>
                    <apex:selectOption itemLabel="CAD" itemValue="CAD"></apex:selectOption>
                    <apex:selectOption itemLabel="AUD" itemValue="AUD"></apex:selectOption>
                </apex:selectList>
            </apex:pageBlockSection>
            
              <!-- More code to create tables -->

        </apex:pageBlock>
    </apex:form>
</apex:page>

CONTROLLER
 
public class InvoicesReadyToPayV4 {
    public List<GEOinvoice__c> geoInvoices {get;set;}
    public List<Account> localPartners {get;set;}
    public Set<Id> localPartnerIds = new Set<Id>();
    public Map<Account, Decimal> LocalPartnerSum {get;set;}
    public Map<Account, String> LocalPartnerReady {get;set;}
    
    public String currencyPickvalue {get;set;}
    public String selectedCurrency {get;set;}
    //selectedCurrency = 'USD';	//Default
        
    public void getCurrency() {
        selectedCurrency = currencyPickvalue;
        System.debug('Selected Currency: ' + selectedCurrency);
        //Do I have to re-run the below method?
	}
    
    //Find all invoices to be shown in report
    public InvoicesReadyToPayV4() {
       
        geoInvoices = [SELECT Id, Name
                                    FROM GEOInvoice__c
                                   WHERE LP_invoice_currency__c = :selectedCurrency];

        //More code related to the tables
   }
}

I think what I'm trying to do is Click "Go", it passes the selected value to the "getCurrency" method, which initializes a string value for the currency in Apex so I can bind it in the SOQL query. Then the getCurrency method will run the main method again to re-render the page.

It looks like there's a few bumps I can't seem to figure out, and I've been racking my brains on this for ages!

Thanks in advance! 


 
Best Answer chosen by Jacky Lee
Alain CabonAlain Cabon
Hi,

Your code works fine. 

rerender => id of the part of the form.

<apex:commandButton value="Go" reRender="Render" action="{!CurrencyGenerate}"/>

<apex:outputPanel id="Render">
 
<apex:page controller="InvoicesReadyToPayV4" tabStyle="GEOinvoice__c">
    <apex:form >
        <apex:pageBlock title="Test GEO Invoice Report">
            <apex:pageBlockSection columns="1">
                Select Currency:
                <apex:selectList value="{!currencyPickvalue}" size="1">
                    <apex:selectOptions value="{!CurrencyValue}"/>
                </apex:selectList>
                <apex:commandButton value="Go" reRender="Render" action="{!CurrencyGenerate}"/>
            </apex:pageBlockSection>
            <apex:outputPanel id="Render">
                <apex:outputText value="selected value:{!currencyPickvalue}"></apex:outputText>
            </apex:outputPanel>
<!-- Additional code to create tables-->

</apex:pageBlock>
    </apex:form>
</apex:page>
public PageReference CurrencyGenerate() {
        System.debug('Page Refreshed:' + currencyPickvalue);
        return null;
 }

What is your problem currently?

All Answers

Alain CabonAlain Cabon
Hi,

As you know, that was not a button but:  <apex:actionSupport event="onchange" action="{!getleads}" rerender="table1"/>
It was an action support for the <apex:selectList> but for a button, it is different.

Your <apex:commandButton value="Go"> should be outside <apex:selectList> like the solution here:
https://salesforce.stackexchange.com/questions/23718/rerender-based-on-selectoption-value-on-commandbutton-click
Jacky LeeJacky Lee
Hi Alain,

I have moved the button outside the List and it points to a PageReference. I have also moved the list to Apex as they did in your example.

However I'm still having issues passing in the selected option into Apex. I think it is the commandButton not actually putting it in apex. I'm wondering how I can pass the selected value into the InvoicesReadyToPayV4() method so it can run the SOQL query using the new "currencyPickvalue".

VF
<apex:page controller="InvoicesReadyToPayV4" tabStyle="GEOinvoice__c">
    <apex:form >
        <apex:pageBlock title="Test GEO Invoice Report">
            <apex:pageBlockSection columns="1">
                Select Currency:
                <apex:selectList value="{!currencyPickvalue}" size="1">
                    <apex:selectOptions value="{!CurrencyValue}"/>
                </apex:selectList>
                <apex:commandButton value="Go" reRender="Render" action="{!CurrencyGenerate}"/>
            </apex:pageBlockSection>
             
​              <!-- Additional code to create tables-->

</apex:pageBlock>
    </apex:form>
</apex:page>


Apex
public class InvoicesReadyToPayV4 {
    public List<GEOinvoice__c> geoInvoices {get;set;}
    
    public String currencyPickvalue {get;set;}
    
    public List<SelectOption> getCurrencyValue() {
        List<SelectOption> CurrencyOptions = new List<SelectOption>();
        CurrencyOptions.add(new SelectOption('USD', 'USD'));
        CurrencyOptions.add(new SelectOption('CAD', 'CAD'));
        CurrencyOptions.add(new SelectOption('AUD', 'AUD'));
        CurrencyOptions.add(new SelectOption('EUR', 'EUR'));
        return CurrencyOptions;
    }
    
    public PageReference CurrencyGenerate() {
        System.debug('Page Refreshed.');
        return null;
    }
    
    public InvoicesReadyToPayV4() {
        currencyPickvalue = 'USD';
        
        //Find all invoices in that would show up in the report.
        geoInvoices = [SELECT Id, Name
                                    FROM GEOInvoice__c
                                 WHERE LP_invoice_currency__c =:currencyPickvalue];

        //Other code

      }
}

 
Alain CabonAlain Cabon
Hi,

Your code works fine. 

rerender => id of the part of the form.

<apex:commandButton value="Go" reRender="Render" action="{!CurrencyGenerate}"/>

<apex:outputPanel id="Render">
 
<apex:page controller="InvoicesReadyToPayV4" tabStyle="GEOinvoice__c">
    <apex:form >
        <apex:pageBlock title="Test GEO Invoice Report">
            <apex:pageBlockSection columns="1">
                Select Currency:
                <apex:selectList value="{!currencyPickvalue}" size="1">
                    <apex:selectOptions value="{!CurrencyValue}"/>
                </apex:selectList>
                <apex:commandButton value="Go" reRender="Render" action="{!CurrencyGenerate}"/>
            </apex:pageBlockSection>
            <apex:outputPanel id="Render">
                <apex:outputText value="selected value:{!currencyPickvalue}"></apex:outputText>
            </apex:outputPanel>
<!-- Additional code to create tables-->

</apex:pageBlock>
    </apex:form>
</apex:page>
public PageReference CurrencyGenerate() {
        System.debug('Page Refreshed:' + currencyPickvalue);
        return null;
 }

What is your problem currently?
This was selected as the best answer
Jacky LeeJacky Lee
Hi Alain,

Thank you very much for your patience, it works now! 

I was having trouble rerendering the page, so I removed reRender and it worked fine, although it reloaded the page every time. Then I tried putting the id = "Render" in the <apex:form id="Render">, and it reRendered the whole page on change. That's exactly what I needed.

Also, to get my code working I had to make my method void to call it from the commandButton.

Thanks!