+ Start a Discussion
smitha george 5smitha george 5 

Creating Visualforce Page with custom picklist

Hi Guys,

I have a requirement ..i  need to create a visualforce page which will display all account names in custom picklist.
Please help me on this.

Thanks in Advance.
Smitha
Best Answer chosen by smitha george 5
GauravendraGauravendra
Hi Smitha,

 Visualforce:
<apex:page controller="dvcm5Controller" >
    
    <apex:pageBlock title="Account-Opportunity">
        <apex:form >
            <apex:pageBlockSection title="Account">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account" /> 
                    <apex:selectList value="{!selectedAccount}" size="1"  >
                        <apex:selectOptions value="{!accountOption}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
      </apex:form>
    </apex:pageBlock>
</apex:page>

Controller:
 
public class dvcm5Controller {

    public List<SelectOption> accountOption{set;}
    public List<Account> accList {get;set;}
    public String selectedAccount{get;set;}

    public List<SelectOption> getAccountOption () {
        List<SelectOption> options = new List<SelectOption>();
        accList = [Select Id,Name From Account];
        for(Account acc : accList) {
            options.add(new SelectOption(acc.Id,acc.Name));
        }
        return options;
    }
    
}

Hope this helps.

All Answers

GauravendraGauravendra
Hi Smitha,

 Visualforce:
<apex:page controller="dvcm5Controller" >
    
    <apex:pageBlock title="Account-Opportunity">
        <apex:form >
            <apex:pageBlockSection title="Account">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Account" /> 
                    <apex:selectList value="{!selectedAccount}" size="1"  >
                        <apex:selectOptions value="{!accountOption}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
      </apex:form>
    </apex:pageBlock>
</apex:page>

Controller:
 
public class dvcm5Controller {

    public List<SelectOption> accountOption{set;}
    public List<Account> accList {get;set;}
    public String selectedAccount{get;set;}

    public List<SelectOption> getAccountOption () {
        List<SelectOption> options = new List<SelectOption>();
        accList = [Select Id,Name From Account];
        for(Account acc : accList) {
            options.add(new SelectOption(acc.Id,acc.Name));
        }
        return options;
    }
    
}

Hope this helps.
This was selected as the best answer
smitha george 5smitha george 5

Yes Guravendra its working.

Thanks a lot.