0

I have the below VF page where I am trying to list the account and contact details as radio button options:

<apex:page standardController="Account" recordSetVar="accounts">
    <apex:form >



<apex:pageBlock title="Account List" id="account_list">


<table>
  <apex:repeat value="{!accounts }" var="acc">
    <tr> 
      <td>
       <apex:selectRadio layout="pageDirection">
      <apex:selectOptions value="{!acc.Name}"/>
      </apex:selectRadio >
      </td>
    </tr>
  </apex:repeat>
</table>


<h1> Contacts</h1>

<table>
  <apex:repeat value="{!accounts }" var="acc">
    <tr> 
      <apex:repeat value="{!acc.Contacts}" var="cont">
      <td><apex:outputText value="{!cont.Name}"/></td>
      </apex:repeat>
    </tr>
  </apex:repeat>
</table>


</apex:pageBlock>

    </apex:form>
</apex:page>

When I preview I get an error which says Invalid selectOptions found. Use SelectOption type in Apex. I would like to display the list of account and I should I able to use radio button to select a particular account. How can achieve this?

1
  • Isn't this a standard controller - Account?
    – SfdcBat
    Feb 9, 2016 at 17:53

2 Answers 2

2

You may need to create a SelectOption list using a controller extension, something like this:

public List<SelectOption> getItems() {
  List<SelectOption> options = new List<SelectOption>(); 
  for (Account a : myAccountsList) {
    options.add(new SelectOption(a.Id, a.Name))
  }

  return options; 
}

Then you add the options to your <apex:selectRadio> tag thusly:

<apex:selectRadio>
  <apex:selectOptions value="{!items}" />
</apex:selectRadio>

See also the documentation on apex:selectRadio.

5
  • Thank you so much. can you let me know where do you get myAccountslist from ? in (Account a : myAccountsList)
    – SfdcBat
    Feb 9, 2016 at 20:58
  • You'll need to query the Account object for the records. Take a look at the sample code in the apex:selectRadio documentation - it has an example for contacts, you can modify it to query account instead. Feb 9, 2016 at 20:59
  • Wouldn't it come from the StandardSetController.getSelected() results?
    – Adrian Larson
    Feb 9, 2016 at 21:07
  • Thanks. I works now! Only thing is all the accounts appears in a single row instead of appearing as list. I think its something to do with the table : Here is the code : <table> <tr> Accounts <td> <apex:selectRadio > <apex:selectOptions value="{!items}" /> </apex:selectRadio> </td> </tr> </table>
    – SfdcBat
    Feb 9, 2016 at 21:29
  • Take a look at the 'layout' attribute on <apex:selectRadio>. As I mentioned in another comment (I think on your related question), <apex:selectRadio> renders the radio buttons in a table. The 'layout' attribute allows you to run them vertically. Feb 9, 2016 at 21:33
1

To make a selectRadio work without extensions, you need to build out SelectOption markup.

<apex:selectRadio>
    <apex:repeat value="{!accounts}" var="account">
        <apex:selectOption itemValue="{!account.Id}" itemLabel="{!account.Name}" />
    </apex:repeat>
</apex:selectRadio>
2
  • I get this error - Select components should have at least one child component of type selectOption or selectOptions
    – SfdcBat
    Feb 9, 2016 at 17:58
  • It appears as radio button when you do not use repeat tag in between selectRadio and SelectOption tag. But I am able to check all the radio button(accounts) at the one time, I guess I should only be able to check one account at a time since its a radio button. Correct?
    – SfdcBat
    Feb 9, 2016 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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