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
RasiRasi 

Creating a pickList with a list of data from the database.

Hi,

 

I want to create a picklist with the data from the database. How can i create it?

Best Answer chosen by Admin (Salesforce Developers) 
jaxdmasterjaxdmaster

Apex Controller method

    public List<SelectOption> getCountrySelectOptions() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('', ''));
		List<Country__c> countries = [Select Code,Name From Country];
		for(Country__c country : countries) {
			String code = country.Code;
			String name = country.Name;
		
			options.add(new SelectOption(code, name);
		}
		return options;
    }

 VF Page

		<apex:selectList value="{!selectedCountry}" size="1">
		<apex:selectOptions value="{!countryOptions}"/>
		</apex:selectList>

 selectedCountry is a property of controller and countryOptions is above method in controller.

All Answers

jaxdmasterjaxdmaster

Poor description!!!:mansad:

 

Can you please elaborate in depth, what you exactly want?

RasiRasi

Simply, we can create a picklist through a field which its type is Picklist. And there we can define the list of values. But what i want is to get this list from the databse and display in the picklist.

jaxdmasterjaxdmaster

Apex Controller method

    public List<SelectOption> getCountrySelectOptions() {
		List<SelectOption> options = new List<SelectOption>();
		options.add(new SelectOption('', ''));
		List<Country__c> countries = [Select Code,Name From Country];
		for(Country__c country : countries) {
			String code = country.Code;
			String name = country.Name;
		
			options.add(new SelectOption(code, name);
		}
		return options;
    }

 VF Page

		<apex:selectList value="{!selectedCountry}" size="1">
		<apex:selectOptions value="{!countryOptions}"/>
		</apex:selectList>

 selectedCountry is a property of controller and countryOptions is above method in controller.

This was selected as the best answer
RasiRasi

Thanks For the solution. This does not give a DropDownList. I need somthing like <select> in HTML.

jaxdmasterjaxdmaster

No, it should give select dropdown. Anyways what does it give? I am 100% sure this will generate select dropdown. I think you are doing something wrong.

RasiRasi

Thanks jaxdmaster,  it is working.