+ Start a Discussion

Custom Object Picklist populated dynamically



We need to populate a picklist dynamically  in a custom object. When button New is clicked we want the picklist to be populated using SOQL.


Is this possible? How?


Many Thanks,



Sample code:


<apex:page controller="sample">
    <apex:form >
    <apex:pageBlock >
        <apex:commandButton value="Get" action="{!get}"/>
    <apex:pageBlock id = "a" >
        <apex:selectList value="{!answer}" size="1">
            <apex:selectOptions value="{!answers}"/>




public class sample
    public List<Account> act = new List<Account>();
    public List<String> values = new List<String>();
    public String answer;

    public void get()
        String sql = 'SELECT Name FROM Account LIMIT 10';
        act = Database.Query(sql);
        for(Account a:act)

    public List<SelectOption> getanswers()
        List<SelectOption> options = new List<SelectOption>();
        for(String s:values)
            options.add(new SelectOption(s,s));
        return options;
    public String getanswer()
        return answer;
    public void setanswer(String answer)
        this.answer = answer;


If this is the solution, kindly mark this as solution.


Cheers !!!