0

This code compiles but in preview, it shows an error Invalid selectOptions found. Use SelectOption type in Apex.

public list<student__c> myStud = new list<student__c>();
   List<student__c> stud_qry = new List<student__c>();
    public list<student__c> getStud(){
        stud_qry =  [select Id, First_Name__c, name from Student__c];  
       for(student__c getEm : stud_qry){
          myStud.add(new student__c(Name = getEm.first_name__c+' ' + getEm.name));
           }
           return myStud;
   }

VFP

<apex:outputText >Select Student:</apex:outputText>
                <apex:selectList size="1">

                    <apex:selectOptions value="{!Stud}"/>
                </apex:selectList>
4
  • Check you code again your above code not contain any selectOption? Are you creating selectOption in apex controller ?
    – Ratan Paul
    Feb 15, 2016 at 6:47
  • could you post you visualforce code that contains the selectlist...
    – Micz Domz
    Feb 15, 2016 at 6:48
  • There is no method start added in your post. Please add the complete method. Also post the part of SelectList code which you are using on the page.
    – ajinkyah
    Feb 15, 2016 at 6:48
  • no, i dont have any selectOption in my controller Feb 15, 2016 at 6:59

3 Answers 3

1

try this...

 public List<SelectOption> getStud() {
  List<SelectOption> myStud = new List<SelectOption>();
  for(student__c getEm : [select Id, First_Name__c, name from Student__c]) {
      myStud.add(new SelectOption(getEm.Id, getEm.Name));
  }
  return myStud;      }
1
  • np @gelay...glad to help. I had the same problem before
    – Micz Domz
    Feb 15, 2016 at 7:11
3

You need to create SelectOption List

public list<student__c> myStud = new list<student__c>();
List<student__c> stud_qry = new List<student__c>();
public String strSelectStudentOption    {get;set;}

public list<SelectOption> getStudOption(){
    stud_qry =  [select Id, First_Name__c, name from Student__c];
    list<SelectOption>  lstOptions = new list<SelectOption>();
    for(student__c getEm : stud_qry){
    lstOptions.add(new SelectOption(getEm.first_name__c, getEm.name));
    }
    return lstOptions;
}

And in VF page

<apex:selectList size="1" value="{!strSelectStudentOption}">
    <apex:selectOptions value="{!StudOption}"/>
</apex:selectList>
1
  • Nope. The issue seems to be with the return type of the method used as getter bound to SelectOptions on page. Let us see when the OP posts more details and code. :)
    – ajinkyah
    Feb 15, 2016 at 6:51
0

You should return a List of SelectOption type from getStud() method of your controller as you cannot bind a list of custom object to a apex:SelectOptions.

 public List<SelectOption> getStud() {
    // Construct a list of SelectOptions
    // Return the above list
 }

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.