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
smita bhargavasmita bhargava 

repeated picklist values

I have a code where I am binding account object , Type field values to an custom picklist in visualforce page.
when I run the code many of the picklist values are repeated.
 
public class BindPicklistValues
{
    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)
         {
         system.debug('value of a='+a);
           options.add(new SelectOption(a.ID,a.Type));
         }
       }
       
       return options;
    }
}

<apex:page controller="BindPicklistValues">

    <apex:form >
    
        <apex:selectList value="{!strSelected}" 
                         multiselect="false" 
                         size="1">
                         
            <apex:selectOptions value="{!Values}"/>
            
        </apex:selectList>
    </apex:form>
</apex:page>

Picklist values  'Prospect' is repeated two times , 'Customer-Direct" is repeated 3 times in the picklist.
please let me know the issue.

smita
Prashant Pandey07Prashant Pandey07

Smita- See line 15 where you are querying all the account and adding the type field in the option that is the reason you are getting repeated picklist values from all the account... 
 If you need to limit to one picklist value then try to use this query 
select ID,Type from Account limit 1
If you are working on specific record you may try this class..
 
public class BindPicklistValues
{
    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 -'));
  
      List<Account> acc=[select id,type from account where id='Your Record Current ID'];
      
      
      
      for(Account a:acc)
       {
         if (a != NULL)
         {
         system.debug('value of a='+a);
           options.add(new SelectOption(a.id,a.type));
         }
       }
       
       return options;
    }
}

Please mark this as the best answer!