I have a picklist field. When a user select an Account I would like to store it in a variable (Account_Name). How can this be done?

This is what I have:

 <apex:pageBlockSection title="Account Selection">
    <apex:inputField value="{!Acct.Accounts__c}"/>    

//apex class

 public String Account_Name_v{get;set;}
  • 1
    On a Visualforce page? Can you post what your page looks like thus far?
    – greenstork
    Oct 17, 2013 at 23:42
  • How do people add snippet of their codes on this board? Sorry, I'm new.
    – Caky
    Oct 18, 2013 at 0:31
  • use the button that is a pair of Brackets {}. Highlight the code, and then hit that brackets button to format the code Oct 18, 2013 at 0:32
  • Shortcut... Highlight the code, press Ctrl-K, it'll block it up for you correctly.
    – sfdcfox
    Oct 18, 2013 at 4:03
  • @caky Is your controller standard or extension controller? Oct 18, 2013 at 4:10

1 Answer 1


You can only use <apex:inputField> tags with fields on proper SObjects. When you want to save information to a controller member variable instead you must use one of the more specific alternatives. You can create a picklist of available values and then write it back to your member variable by specifying it as the value of the <apex:selectList> tag.

<apex:pageBlockSection title="Account Selection">
    <apex:selectList value="{!Account_Name_v}">
        <apex:selectOptions value="{!options}"/>

Since this is in a page block section you're probably after standard formatting, in which case you'll want to wrap this input element inside an <apex:pageBlockSectionItem> tag along with a label like so:

<apex:pageBlockSection title="Account Selection">
        <apex:outputLabel for="acctName"/>
        <apex:selectList value="{!Account_Name_v}" id="acctName"
            <apex:selectOptions value="{!options}"/>


public List<SelectOption> GetOptions()
    // You could populate this using a query for accounts
    List<SelectOption> options = new List<SelectOption>
        new SelectOption('acct1', 'Account One'),
        new SelectOption('acct2', 'Account Two'),
        new SelectOption('acct3', 'Account Three')

    return options;
  • 1
    LaceySnr, you're the best. This is my first VF page and you've answered all of my questions I've posted. I really like and find it extremely helpful reading your snippet of codes. Thanks
    – Caky
    Oct 18, 2013 at 21:49
  • Hi LaceySnr, I hoping that you can help with one problem. I'm trying to pass a variable into my SOQL statement. So, I've model my code as you shown above. Here is my SOQL Statement: {List<Design_Activity__c> Designs=[select MMBT_Account__r.ID, MMBT_Account__r.Name,Calendar_Month__c, Calendar_Year__c from Object__c Where MMBT_Account__r.Name =:Account_Name_v and Calendar_Month__r.Month__c = 'September' Limit 5];}
    – Caky
    Nov 5, 2013 at 22:23
  • It doesn't seem that the value from the user selection is being pass down to the variable Account_Name_v. The result for this query is blank. But if I replace the Account_Name_v with an actual account name such as 'First Horizon' then I would the right result.
    – Caky
    Nov 5, 2013 at 22:31
  • Is the account name reset in the getter or anywhere? I've had issues with those being called before the action method is invoked.
    – Matt Lacey
    Nov 5, 2013 at 23:04
  • Then it should definitely have a value in the method with a query. I'd check for conflicting variable names (i.e. a local superceeding the member variable) and that the value chosen definitely matches the field on the record.
    – Matt Lacey
    Nov 5, 2013 at 23:45

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.