2

I want to use the below code to display a custom picklist field's value in a visual force page. Can you suggest how to link the below code with VF page?

          public List<SelectOption> getTypeOptions(){
           List<SelectOption> options = new List<SelectOption>();
           options.add(new SelectOption('None','--None--'));
           Schema.DescribeFieldResult fieldResult = National_Brand_Strategy__c.Strategy_Description__c.getDescribe();
           List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
           for(Schema.PicklistEntry p : ple)
               options.add(new SelectOption(p.getValue(), p.getValue())); 
           return options;
     }

Visulaforce code to bind this list value is as follows -

     <apex:selectList >
        <apex:selectOptions value="{!options}"/>
    </apex:selectList>
1
  • please post your VF page excerpt where you are referencing the property TypeOptions
    – cropredy
    Jan 15, 2015 at 18:02

3 Answers 3

1

the problem is that you have the code in a function. It has to be a property:

public List<SelectOption> regions
{
    get
    {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Project__c.Division__c.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

        for( Schema.PicklistEntry f : ple)
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        return options;
    }
}

Your visual force code is fine, just change your apex code so that it's in a get{ }

1
  • 1
    No it doesn't necessarily have to be a property! Functions work as well. May 3, 2015 at 8:31
3

Just change your VF to this, you should be good to go. Also I hope you have some other property or variable to store the selected value from the picklist, mention that using value parameter in SelectList tag.

<apex:selectList size="1" >
        <apex:selectOptions value="{!TypeOptions}"/>
 </apex:selectList>
0

Not sure what are you asking for. The code provided above seems fine from apex end. It just needs to be plugged with an component in VF page.

1
  • Hi Anshul, pasted the vf part
    – CRM Guy
    Jan 16, 2015 at 2:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .