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 following code -

Page -

<apex:selectList value="{!selectContactId}" multiselect="false" size="1" id="RelatedContact">
    <apex:selectOptions value="{!relatedContacts}"/>
</apex:selectList>

Controller -

public List<SelectOption> getRelatedContacts() 
{
    List<SelectOption> options = new List<SelectOption>();
    options.add(new SelectOption('', '-None-'));
    if(opportunityObj != null && GeneralUtil.CheckBlank(opportunityObj.accountid))
    {
        for(Contact contactObj: [select id,name from contact where accountid=:opportunityObj.accountid limit 999]) //controller can have only 1000 items in list :(
        {
            options.add(new SelectOption(contactObj.id, contactObj.name));
        }
    }
    return options;
} 

this doesn't work for more than 1000 values any ways to get around this ?

share|improve this question
2  
Perhaps only show the picklist after the user has entered at least one filter character. 1000 entries will make your UI unusable. –  CaspNZ Dec 3 '12 at 19:54

2 Answers 2

up vote 11 down vote accepted

I'd say "you're doing it wrong" ;)

Every time you're hitting a limit like that you should stop and ask if that's really what you need to do. Even if you'd be able to input 50K contacts here - what would usability of such monster be? How is user expected to scroll through this? I'd say a lookup with a search function is really the way to go, that's what it is for.

Have you considered using an autocomplete? Jeff Douglas's blog contains a really nice example on dynamic search - it's not exactly autocompletion but you shouldn't have too much trouble modifying it.

An alternative might be @BobBuzzard's VF lookup

Or simply marry jqueryUI autocomplete and a @RemoteAction


But if you insist on reading further... ;)

public class picklistTest{
    public String selectedVal {get;set;}
    public List<SelectOption> getOptions1(){
        List<SelectOption> retVal = new List<SelectOption>();
        for(Integer i = 0; i < 1000; ++i){
            retVal.add(new SelectOption(String.valueOf(i+1), String.valueOf(i+1)));
        }
        return retVal;
    }
    public List<SelectOption> getOptions2(){
        List<SelectOption> retVal = new List<SelectOption>();
        for(Account a : [SELECT Id, Name FROM Account]){
            retVal.add(new SelectOption(a.Id, a.Name));
        }
        return retVal;
    }
    public List<SelectOption> getOptions3(){
        List<SelectOption> retVal = new List<SelectOption>();
        for(Contact c : [SELECT Id, LastName FROM Contact]){
            retVal.add(new SelectOption(c.Id, c.LastName));
        }
        return retVal;
    }
}

<apex:page controller="picklistTest">
<apex:form>
    <apex:selectList value="{!selectedVal}" size="1">
        <apex:selectOptions value="{!options1}"/>
        <apex:selectOption itemValue="Hi StackExchange" itemLabel="Hi StackExchange"/>
        <apex:selectOptions value="{!options2}"/>
        <apex:selectOptions value="{!options3}"/>
    </apex:selectList> 
</apex:form>
</apex:page>

eh

So you can get your query results, build a <List<List<SelectOption>> for example and then have a combo of <apex:repeat> + <apex:selectOptions>... I still advise against doing this though ;)

share|improve this answer
    
I understand in this use case it may be wrong .. but without typeahead, is there any way to get this. (instead of contacts , just consider I want to show picklist of cities more than 1000), is there any way to do it ? –  Prafulla Patil Dec 3 '12 at 21:13
    
I've just checked and it seems selectOption doesn't like having an embedded repeat. You'll really have to offload it to options1, options2 etc I think (data could still sit in a list of lists, just a bunch of copy-paste getters to retrieve it. And I'd go autocomplete / lookup route anyway, based on size of data rather than it's nature ;) "London, United Kingdom" or "London, Ohio, United States"? –  eyescream Dec 3 '12 at 21:43

The limit 1000 is applicable for any List that is shared across the Page and Controller(i.e. public lists). This is because of viewstate size. As everyone suggested, list is definitely not a proper solution to search or select from thousands of values. Just added up my point to mention this is applicable to any list and not only for SelectOption.

share|improve this answer
3  
... unless you use <apex:page readOnly="true"> in which case limit goes up to 10K ;) –  eyescream Jan 29 '13 at 11:34
    
I din know that! +1! –  Vignesh Jan 29 '13 at 13:04

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.