Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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

share|improve this answer
    
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 Jul 16 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 Jul 16 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 Jul 16 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 Jul 16 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 Jul 19 at 1:25

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

share|improve this answer
    
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 Jul 16 at 6:44
    
Small Typo, I assume it should be getOpenPresentations. –  Lieven Juwet Jul 16 at 6:47
    
Cool! glad to help you :) –  highfive Jul 19 at 1:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.