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
T L TEJASWINIT L TEJASWINI 

How to display dynamically custom object record names in a visual force page in picklist

My code is at present hard coded
vf page
======
<apex:page controller="EmployeeDataClass">
 <apex:form >
 <apex:pageBlock title="Department:" >
 <apex:outputText value="{!selectedEmp}" label="you have selected:"/>
  <apex:pageBlockSection title="Custom Picklist Using selectList and selectOptions departments" collapsible="false">
  <apex:selectList value="{!selectedEmp}" multiselect="false" size="1">
                <apex:selectOptions value="{!countriesOptions}"/>
                </apex:selectList>
  </apex:pageBlockSection>              
     <apex:pageBlock title="Employee Data">
         <apex:pageBlockTable value="{!lstEmp}" var="e">
             <apex:column headerValue="Employee Name" >
                 <apex:outputtext value="{!e.Name}"></apex:outputtext>
             </apex:column>
             <apex:column headerValue="Employee City" >
                 <apex:outputtext value="{!e.Email__c}"></apex:outputtext>
             </apex:column>
             <apex:column headerValue="Employee Contact" >
                 <apex:outputtext value="{!e.Phone__c}"></apex:outputtext>
             </apex:column>
              <apex:column headerValue="Employee department" >
                 <apex:outputtext value="{!e.Dept__c}"></apex:outputtext>
             </apex:column>
         </apex:pageBlockTable>
     </apex:pageBlock>
     </apex:pageBlock>
 </apex:form>
</apex:page>

controller
========
public class EmployeeDataClass{

   /*
       constructor is a method which executes automatically when the class is loaded.
       
   */
   public List<Employee__c> lstEmp{get;set;} // Property Declaration.
   public String selectedEmp{get;set;}

   public EmployeeDataClass(){
       lstEmp = new List<Employee__c>(); // Initialization.
       
       lstEmp = [select id,name,email__c,phone__c,dept__c from Employee__c  limit 50000];
         
   }
    public List<SelectOption> getCountriesOptions() {
        List<SelectOption> countryOptions = new List<SelectOption>();
        countryOptions.add(new SelectOption('','-None-'));
        countryOptions.add(new SelectOption('Learning','Learning'));
        countryOptions.add(new SelectOption('HR','HR'));
        countryOptions.add(new SelectOption('Accounts and Finance','Accounts and Finance'));
        countryOptions.add(new SelectOption('Infrastructures','Infrastructures'));
        countryOptions.add(new SelectOption('Research and development','Research and development'));
        countryOptions.add(new SelectOption('Product development','Product development'));
        countryOptions.add(new SelectOption('IT services','IT services'));
        
        return countryOptions;
    }
    public PageReference save(){
        return null;
    }
}

===================
Now I want code in controller to be dynamic.Please suggest any ideas.
Best Answer chosen by T L TEJASWINI
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Below is the sample code. Kindly modify the code as per your requirement.

Visualforce:
<apex:selectList value="{!selectedEmp}" multiselect="false" size="1">                                 
           <apex:selectOptions value="{!countriesOptions}"/>
</apex:selectList>

Controller:
public String selectedEmp {get;set;}

public List<SelectOption> getCountriesOptions() {
        List<SelectOption> empOptions= new List<SelectOption>();
        empOptions.add( new SelectOption('','--Select--'));
        for( Employee__c emp : [SELECT Id, Country__c FROM Employee__c ] ) { // Country__c is API name of a field which you want to display in a picklist
            empOptions.add( new SelectOption(emp.Id, emp.Country__c));
        }
        return empOptions;
    }

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas