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
Sudhakar ReddySudhakar Reddy 

How to retrieve the picklist value

 

 Hi All,

 

 Let us assume that I had one picklist. I had written one method in controller to populate picklist values.

After poulating values and after submitting page,if I want to retrieve the user selected value from that picklist,how can i do it?

 

Please help me out.

Jia HuJia Hu

create a picklist field on Account, API name: Subcategories__c

 

Apex

public with sharing class VF_Picklist_Ctrl {

private final Account account;

public VF_Picklist_Ctrl() {
account = [SELECT Id, Name, Subcategories__c FROM Account
WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}

public Account getAccount() {
return account;
}

public PageReference save() {
System.debug(' ------ User Selection: ' + account.Subcategories__c );
update account;
return null;
}
}

 

VF page:

<apex:page controller="VF_Picklist_Ctrl">
<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Dependent Picklists" columns="2">
<apex:inputField value="{!account.Subcategories__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

test in browser like:

/apex/VF_Picklist1?id=AccountID

ForceMantis (Amit Jain)ForceMantis (Amit Jain)

Hi Sudhakar

 

Jai Hu has answered your question if you have a picklist field on a object. But if you have a visual force page and custom controller on which you want to display a drop-down field with some values and want to get back selected value in controller please refer following code:

 

<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!selectedCountry}">
            <apex:selectOptions value="{!countries}"/>
        </apex:selectList><p/>

        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>

    <apex:outputPanel id="out">
        <apex:actionstatus id="status" startText="testing...">
            <apex:facet name="stop">
                <apex:outputPanel>
                    <p>You have selected:</p>
                    <apex:outputText value="{!selectedCountry}" />
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
</apex:page>

 

Here is controller:

 

public class sampleCon {
        public String selectedCountry {get; set;}
            
        public PageReference test() {
            return null;
        }
            
        public List<SelectOption> getCountries() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }
    }

 

Let me know if this doesn't solve the problem.

 

Sudhakar ReddySudhakar Reddy

Thanks a lot.

 

But here i had one doubt.

 

Here what is the picklist field name in this example?

ForceMantis (Amit Jain)ForceMantis (Amit Jain)

Are you binding your SelectList with a picklist field from a object?