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
Tech EnthuTech Enthu 

SelectOption

How do I set selectoption value dynamically based on query results?

 

I have a single select picklist on my VF page with list values 1,2,3,4,5 and I would like to use this selectlist within datatable to read/write records to database. I am able to write the data to table but when I want to read the data the selectoption is not showing the stored value automatically[still showing the list to select one option]. For sure I must be missing something silly so I would like to request for an example with both controller and VF page code.

 

Thank you

Best Answer chosen by Admin (Salesforce Developers) 
sivaextsivaext

Hi

 

below sample code might help you

 

   page :

  

   <apex:selectList label="Product Family" value="{!selectedList}" size="1" id="selectListServiceType" multiselect="false" >
   <apex:selectOptions value="{!PickListValues}"/> 
   </apex:selectList>

 

  controller:

     productList = all products queried and put into loop.

 

public PageReference getFamilyValues() {

productList7= new List<product2>();
valueList15 = new List<String>();
for(product2 p11:productList)
{
valueList15.add(p11.Family);
}
pickValues7 = new set<String>();
pickValues7.addAll(valueList15);
valueList16 = new List<String>();
valueList16.addAll(pickValues7);
valueList16.sort();
system.debug('..................'+valueList16);
PickListValues = new List<SelectOption>();
PickListValues.add(new SelectOption('--None--','--None--'));
if(valueList16.size()>0)
{
for(String s:valueList16) {
if(s!=null){
PickListValues.add(new SelectOption(s,s));
}
}
}
system.debug('.......................'+PickListValues);

 

 

Note: the above code is sample cde.  i didn't declare any variables.