4

I wanted to save some multiselected values stored in a custom setting and then retrieve the value and display as selected in the selectList.

The first option of saving the value in custom setting is working but i am not sure how we can get the values as selected.

Any pointers on how this can be done would be a great help.

<apex:pageBlockSectionItem id="pbsiStatus">
                <apex:outputlabel value="Don’t Show Leads with Status"  id="lblStatus" /> 
                <apex:selectList id="selectStatus" value="{!cls.Dont_Show_Lead_With_Status__c}" multiselect="true" styleClass="convstatus" >
                    <apex:selectOptions value="{!statusList}"/>
                </apex:selectList>
 </apex:pageBlockSectionItem>

The saved value is stored as [Closed - Converted, Working - Contacted]

Update: As suggested by @Keith, i am changing the way to store in the custom setting as Working - Contacted; Open - Not Contacted by removing the "[" and "]" and delimiter as semi-colon instead of comma.

I still cant see the values as default selected when i view the VF page.

Update 2: If i select just 1 item from the selectList then i can see the value is selected. But there is no selection if more than one option is selected.

2 Answers 2

6

In your controller initialize Dont_Show_Lead_With_Status__c to all the option values (obtained from your custom setting) separated with a semi-colon using something like:

String[] optionValues = ...;

cls.Dont_Show_Lead_With_Status__c = String.join(optionValues, ';');

so they are all pre-selected.

PS Just saw you addition about the format they are stored in; I've only every seen the semi-colon used to separate the multiple selected values.

7
  • I guess i can change the way its saved to use semi-colon. I have also used multiselect and it saved into database seperated by semi-colon but all of those were saving into a picklist value and not a Text datatype
    – Prady
    Commented Jul 8, 2015 at 12:42
  • @Prady Right, I was assuming the field was of multi-select picklist type.
    – Keith C
    Commented Jul 8, 2015 at 12:59
  • i can see the selected value is selected if there is only one option selected, but none are preselected if there is more than one option selected. Any thoughts why?
    – Prady
    Commented Jul 8, 2015 at 14:13
  • @Prady My guess is that packing the selections in separated by semi-colons is tied to the field type not the multiselect="true". Suggest you change the field type.
    – Keith C
    Commented Jul 8, 2015 at 14:31
  • unfortunately i am tied up with custom settings . And i dont have an option of using picklist in a custom setting.
    – Prady
    Commented Jul 8, 2015 at 15:22
1

The Multiselect picklist is just the list of string. So cls.Dont_Show_Lead_With_Status__c can take list of string as parameter.

In your constructor,

List<String> dontshowstatus = new List<String>();
for(Custom_Settings__c c : Custom_Settings__c.getValues()) {
    dontshowstatus.add(c.Name);
cls.Dont_Show_Lead_With_Status__c = dontshowstatus;

The values from custom settings will be assigned to list of string and then assigned to cls.Dont_Show_Lead_With_Status__c.

Hope it helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .