1

I am binding account object field type values to a Picklist.

But when I run the Visualforce Page I see repeated values, for ex. Customer-Direct value is repeated 4 times. Why could that be? How do I fix it?

Class:

public class someclass {
    public String strSelected{get;set;}

    public List<SelectOption> options{get;set;}

    public List<SelectOption> getValues() {
        strSelected='';

        options=new List<SelectOption>();
        options.add(new selectOption('', '- None -'));
        for(Account a:[select ID,Type from Account]) {
            if (a != NULL) {
                options.add(new SelectOption(a.ID,a.Type));
            }
        }
        return options;
    }

Page:

<apex:page controller="someclass">
    <apex:form >
        <apex:selectList value="{!strSelected}" 
            multiselect="false" 
            size="1">
            <apex:selectOptions value="{!Values}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Dec 20, 2017 at 17:08

1 Answer 1

0

Instead of running a query to get the values from the database, try getting the values of your picklist from your schema. Once you have a list of your picklist values, you can easily sort, filter, or perform any other operation on your data based on these values.

For example, putting your records into a Map<String, List<Account>>, where the String is a valid type value. From here, its easy enough to iterate over each key & display the lists of accounts in a page block table.

Taken from a Developer Article:

Lets say we have a custom object called OfficeLocation__c. OfficeLocation__c contains a number of fields, one of which is a picklist of country values called, creatively enough, Country__c. Our customer requirements are to include the picklist of countries on a Visualforce page which uses a custom controller. The first thing we need to do, within our controller is use the getDescribe() method to obtain information on the Country__c field:

Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe();

We know that Country__c is a picklist, so we want to retrieve the picklist values:

List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

The only thing left for us to do is map the picklist values into an <apex:selectOptions> tag can use for display. Here is the entire method from our controller to do this:

public List<SelectOption> getCountries()
{
  List<SelectOption> options = new List<SelectOption>();

   Schema.DescribeFieldResult fieldResult = OfficeLocation__c.Country__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

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

You must log in to answer this question.

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