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
sourabh kalvasourabh kalva 

can we write the same code without the command buttons and get the record from sobject

VF page;

<apex:page controller="customPickListInVFDemoController" tabStyle="Account">
  <apex:form >
    <apex:pageBlock title="Custom PickList Demo" id="out">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="out" status="actStatusId"/>
            <apex:actionStatus id="actStatusId">
                <apex:facet name="start">
                    <img src="/img/loading.gif" />
                </apex:facet>
            </apex:actionStatus>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Custom Picklist Using selectList and selectOption" collapsible="false">
            <apex:selectList value="{!selectedCountry1}" multiselect="false" size="1">
                <apex:selectOption itemValue="INDIA" itemLabel="India"/>
                <apex:selectOption itemValue="USA" itemLabel="USA"/>
                <apex:selectOption itemValue="United Kingdom" itemLabel="UK"/>
            </apex:selectList>
             
            <apex:outputText value="{!selectedCountry1}" label="You have selected:"/>
        </apex:pageBlockSection>
         
        <apex:pageBlockSection title="Custom Picklist Using selectList and selectOptions" collapsible="false">
            <apex:selectList value="{!selectedCountry2}" multiselect="false" size="1">
                <apex:selectOptions value="{!countriesOptions}"/>
            </apex:selectList>
             
            <apex:outputText value="{!selectedCountry2}" label="You have selected:"/>
        </apex:pageBlockSection>
    </apex:pageblock>
  </apex:form>
</apex:page>



controller;
public class customPickListInVFDemoController {
public String selectedCountry1{get;set;}
public String selectedCountry2{get;set;}
    public customPickListInVFDemoController(){
    }
     
    public List<SelectOption> getCountriesOptions() {
        List<SelectOption> countryOptions = new List<SelectOption>();
        countryOptions.add(new SelectOption('','-None-'));
        countryOptions.add(new SelectOption('INDIA','India'));
        countryOptions.add(new SelectOption('USA','USA'));
        countryOptions.add(new SelectOption('United Kingdom','UK'));
        countryOptions.add(new SelectOption('Germany','Germany'));
        countryOptions.add(new SelectOption('Ireland','Ireland'));
 
        return countryOptions;
    }
    public PageReference save(){
        return null;
    }
}
mukesh guptamukesh gupta
HI Sourabh,

Here command button using to save pickilist values , 

if you want to get by sObject then please follow below code:-

Apex class:- 
 
public class productselect{
public String selectedVal{get;set;} 

public list<SelectOption> getProductnames()
{
  list<SelectOption> soList = new list<SelectOption>();
  soList.add(new SelectOption('', '- None -'));
  for(Product prod : [Select Id, product_type__c from Product LIMIT 1000])
  soList.add(new SelectOption(prod.id, Prod.product_type__c));
  return soList;
}
}

VF Page:-
 
<apex:page controller="productselect">
<apex:form >
<apex:selectList value="{!selectedVal}" size="1"> 
<apex:selectOptions value="{!Productnames}" /> 
</apex:selectList>
</apex:form>
</apex:page>

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh