1

So I have a dropdown that is appended with options within controller. I also have a filter method which is supposed to use the value of the dropdown as a filter for the form.

How can I reference values from the UI when an action calls a method from a controller:

Button click calls filter -> filter filters the vars -> I need to pass whatever is in SelectList (businessUnitControl)

VF Page:

<apex:pageBlockSection title="Filter here" collapsible="true" columns="1">
   <apex:outputLabel value="Select Business Unit " for="businessUnitControl"/>
   <apex:selectList size="1" id="businessUnitControl"> 
      <apex:selectOptions value="{!Units}"/>
   </apex:selectList>
   <apex:commandButton action="{!filter}" value="filter"/>              </apex:pageBlockSection>

Controller

public void filter(){
        setCon = [SELECT  Name, business_unit__c 
                  FROM APM__c 
                  where business_unit__c=:???something from vf page???
                  limit 1];
}

public List<SelectOption> getUnits()
{
   List<SelectOption> options = new List<SelectOption>();
   List<Business_Unit__c> units = new List<Business_Unit__c>();
   units = [SELECT Name FROM Business_Unit__c];

   for (Business_Unit__C b: units)
   {
     otions.add(new selectOption(b.Name, b.Name));
   }

   return options;
}

1 Answer 1

1

First, in your controller, specify a variable to hold the input value:

public transient String filterBusinessUnitControl { get; set; }

Note that { get; set; } is strictly required to get/set values in your controller from Visualforce. Since this variable is bound to an input, we can use transient to reduce the size of your page state. However, if you get weird behavior, you may need to remove the transient keyword.

Next, alter your page:

<apex:selectList size="1" id="businessUnitControl" value="{!filterBusinessUnitControl}">

This creates a two-way binding between the page's select list and the variable in the controller (meaning, changes in the controller will also reflect back to the page).

Third, simply bind your query to the parameter:

setCon = [SELECT  Name, business_unit__c 
              FROM APM__c 
              where business_unit__c=:filterBusinessUnitControl
              limit 1];
4
  • you sir, are killing it!!!
    – tjcinnamon
    Apr 14, 2016 at 17:54
  • Is there a way to reference the logged in users account as well? That's not on a control on the form.
    – tjcinnamon
    Apr 14, 2016 at 18:02
  • 1
    @tjcinnamon See UserInfo for things you can get about the current user without a query. You can query additional details from the database if you need to using the User, Profile, and UserRole objects.
    – sfdcfox
    Apr 14, 2016 at 18:05
  • @sfdcfox When using the Variable to store the value say for a input checkbox , when the page is refreshed will it set back to unchecked. even the user checked the checkbox hit the save button?. I should store the selection/ deselection in to the varaiable but show the same even when page is reloaded how can that be achieved
    – user81642
    Jul 13, 2020 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.