+ Start a Discussion
QuickDEVQuickDEV 

Constructing <selectOption> dynamically

Hi all,

 

I am trying to construct a <selectOption> dynamically by reading the values from a custom object which will be displayed in VF page.

 

Here is my code:

 

 

public List<SelectOption> getZipList () { List<SelectOption> zipList = new List<SelectOption>(); List<Zip_List__c> zipListObj= new List<Zip_List__c>(); zipListObj = [Select Name From Zip_List__c]; for (integer i=0;i<zipListObj.size;i++) { zipList.add(new SelectOption(zipListObj[i],zipListObj[i])); } }

 When I try to save the controller, it throws:

 

 Error: Compile Error: Constructor not defined: [System.SelectOption].<Constructor>(SOBJECT:Zip_List__c, SOBJECT:Zip_List__c)

 

If I hardcode the values in " zipList.add(new SelectOption(zipListObj[i],zipListObj[i])); ", it is working fine for me,

 

Please advice. 

 

Thanks in Advance

 

Best Answer chosen by Admin (Salesforce Developers) 
jeffdonthemic2jeffdonthemic2

Here's a code snippet that might help:

 

 

public List<SelectOption> getLevel1Items() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('','-- Choose a Group --')); for (Service_Group__c g : [select Id, Name, Grp_ID__c from Service_Group__c Order By Name]) { options.add(new SelectOption(g.Id,g.Grp_ID__c+' - '+g.Name)); } return options; }

 

 

 

 Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com 

 

All Answers

jeffdonthemic2jeffdonthemic2

Here's a code snippet that might help:

 

 

public List<SelectOption> getLevel1Items() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('','-- Choose a Group --')); for (Service_Group__c g : [select Id, Name, Grp_ID__c from Service_Group__c Order By Name]) { options.add(new SelectOption(g.Id,g.Grp_ID__c+' - '+g.Name)); } return options; }

 

 

 

 Jeff Douglas

Appirio, Inc.

http://blog.jeffdouglas.com 

 

This was selected as the best answer
Sivakumari2iSivakumari2i


Hi,

Here's a code snippet i used similar to your last post.

But i am getting a error as "Error: Compile Error: Method does not exist or incorrect signature: options.add(System.SelectOption) at line 9 column 11"

 

public class picklistTest
{
    public List<SelectOption> getOptions2()
    {
        List<SelectOption> retVal = new List<SelectOption>();
        retVal.add(new SelectOption('','-- Choose a Group --'));        
        for(Period a : [Select Id, type, Number, CALENDAR_YEAR(StartDate) s from Period where Id in (Select PeriodId from ForecastingQuota)])
        {
          options.add(new SelectOption(a.Id,a.type+' - '+a.Number));                 
        }
        return retVal;
    }
}

 


Please help me.

 

Regards,

S.Sivakumar

Sivakumari2iSivakumari2i

I found the error in my code snippet

Thanks for your help

B. Wade LovellB. Wade Lovell
The error was here:
{
          retVal.add(new SelectOption(a.Id,a.type+' - '+a.Number));                 
        }
        return retVal;
    }
}

In this case, the OP had 
options.add(new SelectOption(a.Id,a.type+' - '+a.Number)); instead of 
retVal.add(new SelectOption(a.Id,a.type+' - '+a.Number));