+ Start a Discussion
navanitachoranavanitachora 

Error: Compile Error: Constructor not defined: [System.SelectOption].

Hi,

 

I am very new to Salesforce but have Java and Python Web programming experience and have got the problem in the subject line.

 

My apex controller code is:

 

public class TimesheetEntry {
    public List<SelectOption> options;
    public TimesheetEntry() {
    }

    public List<SelectOption> getAccountItems() {
        List<SelectOption> options = new List<SelectOption>();
        for (Account account: [SELECT name, id FROM Account]) {
            options.add(new SelectOption(account.name));
        }
        return options;
    }
}

 

for some reason it is complaining about the line:

 

options.add(new SelectOption(account.name));

 I have seen a number of forum posts where the reason for the error was that what was sent to SelectOption was not a string but in this case account.name is a string. I have also tried type casting but that has not worked.

 

I would be most grateful for any pointers.

 

Many thanks.

Best Answer chosen by Admin (Salesforce Developers) 
colemabcolemab

The constructor for the select option expects 2 arguments (value and label).   Since the constructor isn't overloaded, the system can't find the constructor that accepts only one argument and that is why you are getting the error.

 

Assuming account.name is unique, try this:

 

options.add(new SelectOption(account.name,account.name));

 

However, you probably want to use the account ID as the value as it would be truly unique.

All Answers

colemabcolemab

The constructor for the select option expects 2 arguments (value and label).   Since the constructor isn't overloaded, the system can't find the constructor that accepts only one argument and that is why you are getting the error.

 

Assuming account.name is unique, try this:

 

options.add(new SelectOption(account.name,account.name));

 

However, you probably want to use the account ID as the value as it would be truly unique.

This was selected as the best answer
navanitachoranavanitachora

Thanks that did the trick.

 

I should have looked at the documentation more carefully.