Friday, October 16, 2015

Converting Multi-Select picklist to Multi-Select checkboxes in VF page.

Converting Multi-Select pick-list to Multi-Select check-boxes in VF page.

In this post i am providing an example of converting Multi Select Pick-list to  convert Multi Select Chyeckboxes in VF page.

For this i created a Multi-Select Pick-list field on opportunity page with some values.

Using custom controller extension in VF page.

Controller Class:

public class MultiCheckBoxExtn {
    public opportunity opp{get;set;}
    public List<SelectOption> leadSourceCheckbox {get;set;}
    public MultiCheckBoxExtn(ApexPages.StandardController controller) {
        opp = (opportunity)controller.getRecord();
    public void saveOpp(){
     upsert opp;
 //get the multi-select pick list values
    public List<SelectOption> MPOptions {
     get {
       List<SelectOption> options = new List<SelectOption>();
       for( Schema.PicklistEntry f : opportunity.Srinivas__Multi_Select_Pick_List__c.getDescribe().getPicklistValues()) {
         options.add(new SelectOption(f.getValue(), f.getLabel()));
       return options;
    //get and set the multi-select pick list as checkboxes
       public String[] MPItems { 
     get {
        String[] selected = new List<String>();
        List<SelectOption> sos = this.MPOptions;
        for(SelectOption s : sos) {
        if (this.opp.Srinivas__Multi_Select_Pick_List__c !=null && this.opp.Srinivas__Multi_Select_Pick_List__c.contains(s.getValue()))
        return selected;
     }public set {
        String selectedCheckBox = '';
        for(String s : value) {
         if (selectedCheckBox == '') 
           selectedCheckBox += s;
         else selectedCheckBox += ';' + s;
        opp.Srinivas__Multi_Select_Pick_List__c = selectedCheckBox;

VF page : 

<apex:page standardController="Opportunity" extensions="MultiCheckBoxExtn" tabStyle="Opportunity" id="thepg">
<apex:pagemessages ></apex:pagemessages>
    <apex:form id="theFm" >
     <apex:pageBlock id="thePB" >
        <apex:pageblockSection title="Opporutnity Details" id="thePbs1" collapsible="false">
         <apex:inputfield value="{!}" id="name1"/>
         <apex:inputField value="{!opp.closeDate}"/>
          <apex:inputField value="{!opp.stageName}"/>

         <apex:pageBlockSection title="Multiselect Picklist to Checkbox" columns="2" collapsible="false" id="thePbs2">
         <apex:outputLabel value="{!$ObjectType.opportunity.Fields.Multi_Select_Pick_List__c.InlineHelpText}" />
           <apex:selectcheckboxes layout="pageDirection"  value="{!MPItems}" label="" id="checkbox1">                   
             <apex:selectoptions value="{!MPOptions}"  > </apex:selectoptions>       
        <apex:pageblockButtons >
         <apex:commandButton value="Save" action="{!saveOpp}"/>

Once the user clicks  on the save button it will set the check box values into multi select pick-list 

If you edit the record it will show the multiselect picklist like this.

That's it.



Unknown said...

can this be done using records rather picklists?

Shaolin Dave said...

Nice, seems to be a couple problems though:

The field name is hard-coded (in this case, "Srinivas__Multi_Select_Pick_List__c"), it'd make more sense to write it to be reusable. I'm researching this as I'm researching a visualforce page with dozens of multi-picklist fields.

Also, I see it being used to populate the values of the picklist from the visualforce page, but it seems the visualforce checkboxes will always show as "false". Fully-functional checkboxes would start with the proper values checked.

srujana dasari said...

I am trying your code.I got Error like this,
System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updateAccountIfOppCustomer: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.updateAccountIfOppCustomer: line 14, column 1: []
Error is in expression '{!saveOpp}' in component in page convertmultipicklisttocheckbox: Class.MultiCheckBoxExtn.saveOpp: line 11, column 1

can you pleace help me....

harry lopiz said...

The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.

Post a Comment

| ,