+ Start a Discussion

Text field as value for multiselect picklist defaulting incorrectly....

Alright I have wound up in a situation where I am using a Text(255) field to field in the value parameter of a SelectList in Visualforce.  When the selectList has multiSelect set to false this works perfectly.  When multiSelect is set to true, it writes the value back to the field exactly as expected.  However if there is already a value in the field, it will not show up preselected.  The value is stored as val1;val2;val3 etc... and is in the same format that the selectList wrote it there as.  Can anyone think of a way to get the selectList to preselect the correct values?  Please do not tell me to use inputField along with the correct field type, trust me that is not an option here. 


From Visualforce Developers Guide:


If multiselect is true, the value attribute must be of type String[] or a List of strings. Otherwise, it must be of type String.

 So im guessing you need to leave multi select as true, and have code that will split on semi colon and bind the resulting array to the select list.


Well a field of type Text(255) is technically just a string, so the question then becomes whats the proper formatting so sfdc recognizes its a list and applies it to the selected values.  A String[] of List<String> is out of the question because the selectList is made in a apex:repeat loop and it appears it will not write to a map in the value parameter. 


O nevermind I read that wrong.  The thing that kills me is it writes the output to the field no problem


Max, did you ever find a solution or workaround to this? I'm in precisely the same situation i.e. I have 3 nested dependent multiselect=true picklists. User selected values/ids are saved perfectly, but then, on an "edit" VF when I bring up the 3 lists, and I cannot seem to get the selections that were saved to highlight in the lists. Thanks.


Yes but it wasn't pretty.  instead of a multi-select picklist I used checkboxes...

<apex:repeat value="{!mplv[aciId + '.' + aci['Field_Name__c']]}" var="key">

<input type="checkbox" style="display:{!IF(mplv[aciId + '.' + aci['Field_Name__c']][key],'none','inline')}" id="{!aciId + '.' + aci['Field_Name__c']}" onchange="setTimeout(changeField(this),0)" name="{!IF(mplv[aciId + '.' + aci['Field_Name__c']][key],'none',aciId + '.' + aci['Field_Name__c'] + 'inline')}" value="{!key}"/>
<input type="checkbox" checked="true" style="display:{!IF(mplv[aciId + '.' + aci['Field_Name__c']][key],'inline','none')}" id="{!aciId + '.' + aci['Field_Name__c']}" onchange="setTimeout(changeField(this),0)" name="{!IF(mplv[aciId + '.' + aci['Field_Name__c']][key],aciId + '.' + aci['Field_Name__c'] + 'inline','none')}" value="{!key}"/>
<label for="{!aciId + '.' + aci['Field_Name__c']}">{!mplCodeTrans[key]}</label>


Just to give you an idea how ugly it gets.  If you still want more details, let me know.  


Ouch! :-( Using CBs is NOT an option for me!


I can get the right options selected and the dependent picklists reflect that, only thing is I CANNOT have those preselected items show up as highlighted!


Thanks for your prompt feedback Max. Appreciate it very much!