1

I have a record that has a multiselect picklist field. I want to display on the VFP all of the different selections. When I load the page with the selectlist or selectcheckboxes, the values do not display.

I have tried both checkboxes and a standard picklist. How am I able to display the saved record values in the VFP? It looks like the values are being returned in a single string seperated by a semicolon ';'

<div class="slds-form-element slds-size--1-of-4">
    <label class="slds-form-element__label" for="select-04">Suspicious Activity</label>
        <div class="slds-form-element__control">
            <apex:selectcheckboxes layout="pageDirection" value="{!selectedSuspiciousActivityOption}">
                <apex:selectoptions value="{!suspiciousActivityOptions}" />
            </apex:selectcheckboxes>
        </div>
 </div>

Debug displaying DB values of Suspicious_Activity__c field

10:37:10.0 (172238954)|USER_DEBUG|[44]|DEBUG|selectedSuspiciousActivityOption:: Banks: banks altered in some way;Business: business is not real

Is there a way around the dueling picklist? If there is not a simple way to just load the page with preexisting picklist data then my thought is I am going to have to split the Suspicious_Activity__c string by the ; and use jQuery to check the appropriate boxes dependent on the values. Is there an easier way to just display on a multiselect picklist all of the selected options?

The picture shows that there should be two checkboxes checked (that data is already in the DB, we want to display this so the user can update later if they want to change the values) enter image description here

  • 3
    What's wrong with just apex:inputField? – sfdcfox Nov 26 '18 at 19:09
2

<apex:selectList> and <apex:selectCheckboxes> both accept a value attribute binding whose data type is List<String>. From the example for <apex:selectCheckboxes>:

Visualforce

    <apex:selectCheckboxes value="{!countries}">
        <apex:selectOptions value="{!items}"/>
    </apex:selectCheckboxes>

Apex

String[] countries = new String[]{};

If you wish to use <apex:selectCheckboxes> to display the value of your multi-select picklist, you'll need to add a property in your Apex controller typed as a List<String>. Call it, say, checkboxSelections:

 public List<String> checkboxSelections;

Then, your code (in Apex, not jQuery) can split the multi-select picklist's value on the semicolon delimiter (;) to populate that property.

checkboxSelections = (String.isNotBlank(myRecord.Multi_Select__c) ? myRecord.Multi_Select__c.split(';') : new List<String>());

Upon performing a save, you'd need to re-join the list:

myRecord.Multi_Select__c = checkboxSelections.join(';');

before performing DML.

  • Awesome thank you. I was able to get it to display on the VFP with a List<String> and a .split and I was able to upsert with a .join. I just needed to break it apart and put it back together so the DB could read it. Thank you! – Olivia Nov 27 '18 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.