2

I have created a custom list using inner classes, this list is called "openPresentations". This list looks like this when I do json.serialize

[{"name": "First Name","id": "a09N0000002R0oeIAC"},

{"name": "Second Name","id": "a09N0000002R0p4IAC"},

{"name": "Third Name","id": "a09N0000002R0dpIAC"},

{"name": "Fourth Name","id": "a09N0000003LxoEIAS"}]

I am trying to add this list into a dropdown in a VF page, with the main purpose of displaying the name in the dropdown and when it is submitted I want to grab the id.

I have unsuccessfully tried something like:

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

but i am keep getting and error "Invalid selectOptions found. Use SelectOption type in Apex. " and if I change to SelectOption i get exactly the same error.

2 Answers 2

7

You have to populate a List<SelectOption> to bind with the UI. Also have a variable to grab which have been selected from the select options. In your controller have below codes

public String selectedVal{get;set;}  // This will hold the selected value, the id in here

public List<SelectOption> getopenPresentationOptions(){
        List<SelectOption> optns = new List<Selectoption>();
        // before getting here you must populate your queryResult list with required fields
        for(Your_Object obj : queryResult){
           optns.add(new selectOption(obj.Id, obj.Name));
        }            

        return optns;
}

VF Page:

<apex:selectList value="{!selectedVal}" size="1"> 
    <apex:selectOptions value="{!openPresentationOptions}" /> 
</apex:selectList>

UPDATE : added code so that selectoptions list will be populated with a query result

5
  • because the list "openPresentations" is a result of a query of two different object (that is why i used an inner class), so i cant hard code the options value, is it possible to loop through "openPresentations" ?
    – manza
    Commented Jul 16, 2014 at 6:55
  • Yes. Add records into the optns list inside a loop so that you can populate it with the query results. See my update
    – highfive
    Commented Jul 16, 2014 at 6:59
  • Sorry still not very clear, I cant understand your comment of "before getting here you must populate your queryResult list with required fields" do you meant that in this section should go the code where I create my list openPresentations? so query result = openPresentations ?
    – manza
    Commented Jul 16, 2014 at 9:29
  • forget that comment and just try out the code. If you can edit your question and add your controller code where you querying these people can help you.
    – highfive
    Commented Jul 16, 2014 at 9:34
  • Thank you highfive, I understand now, I though that i needed to create a list using inner classes, but I tested and seems to work fine if i just type the query there
    – manza
    Commented Jul 19, 2014 at 1:25
3

To bind your options from Apex to your visualforce using apex:selectOptions you need to use the SelectOption object type in Apex.

Your getOpenPresentations method should look something like this:

 public List<SelectOption> getOpenPresentations() {
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('a09N0000002R0oeIAC','Firstname'));
    options.add(new SelectOption('a09N0000002R0p4IAC','Second Name'));
    return options;
}

More information can be found here: https://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_selectoption.htm

3
  • Thank you so much if I copy this i get an error Unknown property. Also this is hard coded as I said at the beginning this string is the result of a query (that is why i am using inner class)
    – manza
    Commented Jul 16, 2014 at 6:44
  • Small Typo, I assume it should be getOpenPresentations. Commented Jul 16, 2014 at 6:47
  • Cool! glad to help you :)
    – highfive
    Commented Jul 19, 2014 at 1:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .