function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TderosaTderosa 

default selected values for a multi-select picklist

I have a multselect picklist in my visualforce page as the following:

 

<apex:selectList size="5" id="selectRev" value="{!selRevRange}" onchange="//doSearch();">
    <apex:selectOptions value="{!RevRange}"/>
</apex:selectList>

 

and the following code in apex to populate them:

 

public String selRevRange {get;set;}
public List<SelectOption> getRevRange() {
List<SelectOption> options = new List<SelectOption>();

options.add(new SelectOption('(AnnualRevenue >= 0 and AnnualRevenue <= 20000000)','$0-20M'));
options.add(new SelectOption('(AnnualRevenue > 20000000 and AnnualRevenue <= 50000000)','$20-50M'));
options.add(new SelectOption('(AnnualRevenue > 50000000 and AnnualRevenue <= 100000000)','$50-100M'));
options.add(new SelectOption('(AnnualRevenue > 100000000 and AnnualRevenue <= 500000000)','$100-500M'));
options.add(new SelectOption('(AnnualRevenue > 500000000 and AnnualRevenue <= 1000000000.00)','$500M-1B'));
options.add(new SelectOption('(AnnualRevenue > 1000000000.00 and AnnualRevenue <= 5000000000.00)','$1-5B'));
options.add(new SelectOption('(AnnualRevenue > 5000000000.00 )','$5B+'));
return options;
}

 

this works fine.   This multiselect picklist is part of a parameter search screen where I want to save the search values and then pull them up at a later time for the user.  The problem is that for the multiselect it only seems to select the optionwhen there is one value selected and saved.  If I save multiple values it won't highlight them in the box when the search record is pulled back up.

 

Here is what gets saved in the database for the picklist when 2 values are stored:

 

[(AnnualRevenue > 20000000 and AnnualRevenue <= 50000000), (AnnualRevenue > 100000000 and AnnualRevenue <= 500000000)]

 

another note: it works with one value if I take out the outer brackets [ ].  For multiple values I have tried replacing the comma with a semicolon ; and a few other things but I can't seem to find a delimiter that works.

 

Any thoughts?

 

Todd

 

Best Answer chosen by Admin (Salesforce Developers) 
jayjaysjayjays

Hi, I cant see where you are assigning selRevRange to the field on the object record or where you are getting it's value and setting selRevRange. The variable should be a string array and split and join used to get and set the values.

 

Public string[] getselRevRange(){
if (c.Test_Multi__c!=null){
return c.Test_Multi__c.split(';');
}
else{
return null;}
}
public void setselRevRange(string[] vals){
c.Test_Multi__c=string.join(vals,';');
}

 

Where c.Test_Multi__c would be the name of the field you are storing the value in.

All Answers

jayjaysjayjays

Hi,

 

I think you need to set multiselect="true" in your selectList and the controller property would need to be an array;

 

<apex:selectList size="5" id="selectRev" value="{!selRevRange}" multiselect="true" onchange="//doSearch();">
    <apex:selectOptions value="{!RevRange}"/>
</apex:selectList>

 

public String[] selRevRange {get;set;}

 

Thanks,

James.

 

TderosaTderosa

Thanks for the reply but I do have multiselect in the visualforce part, for some reason it didn't paste into my last post.  Here is the correct visualforce part:

 

<apex:selectList size="5" multiselect="true" id="selectRev" value="{!selRevRange}" onchange="//doSearch();">
<apex:selectOptions value="{!RevRange}"/>
</apex:selectList>

 

this still doesn't work however.

 

jayjaysjayjays

Hi, I cant see where you are assigning selRevRange to the field on the object record or where you are getting it's value and setting selRevRange. The variable should be a string array and split and join used to get and set the values.

 

Public string[] getselRevRange(){
if (c.Test_Multi__c!=null){
return c.Test_Multi__c.split(';');
}
else{
return null;}
}
public void setselRevRange(string[] vals){
c.Test_Multi__c=string.join(vals,';');
}

 

Where c.Test_Multi__c would be the name of the field you are storing the value in.

This was selected as the best answer
TderosaTderosa
JayJays, this is brilliant. That was the missing link I couldn't figure out. Thank you very much for this explanation!!