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
Mhlangano KhumaloMhlangano Khumalo 

Conversion Error setting value- Showing Multi-select picklist as checkboxes on VF page.

This code displays a multi-select picklist as checkboxes perfectly on a Visualforce page, but when clicking the save button, it shows this error.
Conversion Error setting value 'Its complicated' for '#{Benchmarking_Result__c.Challenges_with_M_E__c}'.

Here is the controller
Public ApexPages.StandardController stdcontroller;

List<SelectOption> Commprefvalue = new List<SelectOption>();
   
    public List<Selectoption> getCommpref()
    {
        Schema.DescribeFieldResult CommprefField = Schema.sObjectType.Benchmarking_Result__c.fields.Challenges_with_M_E__c; 
        Schema.PicklistEntry [] Commpref = CommprefField.getPickListValues();
       
        //Schema.PicklistEntry [] values = vendorField.getPickListValues();
        for(Schema.PicklistEntry val : Commpref)
        {
            Commprefvalue.add(new SelectOption(val.getValue(), val.getLabel()));
        }
        return Commprefvalue;        
    }

//Save button
    public PageReference MAndEDevSaveBtn(){
        stdcontroller.save();
        PageReference callNextPage= Page.CSI_Positioning_And_Governence;
        callNextPage.setRedirect(true);
        return callNextPage;
    }

On the VF page.
<apex:selectCheckboxes value="{!Benchmarking_Result__c.Challenges_with_M_E__c}" layout="pageDirection">
          <apex:selectoptions value="{!Commpref}"></apex:selectoptions>
 </apex:selectCheckboxes>

<apex:commandButton value="Save and Continue" action="{!MAndEDevSaveBtn}" />
The error
User-added image
Best Answer chosen by Mhlangano Khumalo
Mhlangano KhumaloMhlangano Khumalo
Ok, Ive figured it out, with help from http://gautamsfdc.blogspot.com/

my extension
public class benchmarkingExtension_br {

  public Benchmarking_Result__c pr{get; set;}
     //initialize
    public benchmarkingExtension_br (ApexPages.StandardController controller){
      pr=new Benchmarking_Result__c();
    }
   
    //get the multi-select pick list values
    public List<SelectOption> MPOptions {
     get {
       List<SelectOption> options = new List<SelectOption>();
       for( Schema.PicklistEntry f : Benchmarking_Result__c.Challenges_with_M_E__c.getDescribe().getPicklistValues()) {
         options.add(new SelectOption(f.getValue(), f.getLabel()));
        } 
       return options;
     }  
     set;
    }
    
    //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.pr.Challenges_with_M_E__c!=null && this.pr.Challenges_with_M_E__c.contains(s.getValue()))
           selected.add(s.getValue());
        }
        return selected;
     }public set {
        String selectedConcat = '';
        for(String s : value) {
         if (selectedConcat == '') 
           selectedConcat += s;
         else selectedConcat += ';' + s;
        }
        pr.Challenges_with_M_E__c= selectedConcat;
     }
   } 
   
   //create record
   public PageReference savepr() {
        insert pr;
       return (new PageReference('/apex/Innovation?='+pr.Id));
   }

}

VF page
<apex:selectCheckboxes value="{!MPItems}" layout="pageDirection" required="true" id="prodreq">
       <apex:selectOptions value="{!MPOptions}"/>
     </apex:selectCheckboxes>


<apex:commandButton value="Continue" action="{!savepr}" />

IT WORKS!!!!! :) :) :)
 

All Answers

Mhlangano KhumaloMhlangano Khumalo
Ok, Ive figured it out, with help from http://gautamsfdc.blogspot.com/

my extension
public class benchmarkingExtension_br {

  public Benchmarking_Result__c pr{get; set;}
     //initialize
    public benchmarkingExtension_br (ApexPages.StandardController controller){
      pr=new Benchmarking_Result__c();
    }
   
    //get the multi-select pick list values
    public List<SelectOption> MPOptions {
     get {
       List<SelectOption> options = new List<SelectOption>();
       for( Schema.PicklistEntry f : Benchmarking_Result__c.Challenges_with_M_E__c.getDescribe().getPicklistValues()) {
         options.add(new SelectOption(f.getValue(), f.getLabel()));
        } 
       return options;
     }  
     set;
    }
    
    //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.pr.Challenges_with_M_E__c!=null && this.pr.Challenges_with_M_E__c.contains(s.getValue()))
           selected.add(s.getValue());
        }
        return selected;
     }public set {
        String selectedConcat = '';
        for(String s : value) {
         if (selectedConcat == '') 
           selectedConcat += s;
         else selectedConcat += ';' + s;
        }
        pr.Challenges_with_M_E__c= selectedConcat;
     }
   } 
   
   //create record
   public PageReference savepr() {
        insert pr;
       return (new PageReference('/apex/Innovation?='+pr.Id));
   }

}

VF page
<apex:selectCheckboxes value="{!MPItems}" layout="pageDirection" required="true" id="prodreq">
       <apex:selectOptions value="{!MPOptions}"/>
     </apex:selectCheckboxes>


<apex:commandButton value="Continue" action="{!savepr}" />

IT WORKS!!!!! :) :) :)
 
This was selected as the best answer
David9133David9133
@Mhlangano Khumalo : Can you help me with the test class how to pass the selected value in the test class.

Thanks in Advance...
Mhlangano KhumaloMhlangano Khumalo
Check out the example below: @David9133
//The Class
public with sharing class coloursController {
    
    Public Account col{get;set;}       
    
    //Constructor
    public coloursController(ApexPages.StandardController controller) {
        this.col= [select name, Colour__c from Account where Id =: controller.getId()]; //You have to select all the fields you are using

    }

    public List<SelectOption> MPOptions0 {
     get {
       List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('Black','Black'));
        options.add(new SelectOption('White','White'));
        options.add(new SelectOption('Yellow','Yellow'));
        options.add(new SelectOption('Red','Red'));
           
       return options;
     }  
     set;
    }
    
   //get and set the multi-select pick list as checkboxes
    public String[] MPItems0 { 
     get {
        String[] selected = new List<String>();
        List<SelectOption> sos = this.MPOptions0;
        for(SelectOption s : sos) {
        if (this.col.Colour__c !=null && this.col.Colour__c.contains(s.getValue())) //Colour__c is a custom field on Accoount
           selected.add(s.getValue());
        }
        return selected;
     }public set {
        String selectedConcat = '';
        for(String s : value) {
         if (selectedConcat == '') 
           selectedConcat += s;
         else selectedConcat += ';' + s;
        }
        col.Colour__c= selectedConcat;
     }
}

     public PageReference saveBtn() {                       
        update col; 
        
        PageReference callNextPage = new PageReference('/'+col.id);
        callNextPage.setRedirect(true);
        return callNextPage;  
    }
   
}

The Visualforce page
 
<apex:page standardController="Account" extensions="coloursController">
    <apex:form >
        <apex:pageBlock title="Details" >
            <apex:pageMessages />         
            <apex:pageBlockButtons >
              <apex:commandButton value="Save" action="{!saveBtn}"/> 
               <apex:commandButton value="Cancel" action="{!Cancel}"/> 
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" title="Rendering Multi-Select picklists as checkboxes on a Visualforce page" collapsible="false">                
                <apex:inputField value="{!col.Name}"/>  
                     
                <apex:selectCheckboxes value="{!MPItems0}" styleClass="checkboxinput"  layout="pageDirection"   >
                   <apex:selectOptions value="{!MPOptions0}"/>
                 </apex:selectCheckboxes>                                                     
               </apex:pageBlockSection>   
                                                                                           
        </apex:pageBlock>
    </apex:form>
</apex:page>

The Test Class: Doesn't cover all the lines-> has 85% test coverage....You can add more test cases
@isTest
//class name
private class coloursController_Test{
    //test method
    private static testMethod void coloursController_Test(){
        
        Account acc= new Account();            
        acc.Name = 'Account Name';               
        insert acc;
        
        
        //instantiate standardcontroller, pass object variable into constructor
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
       
        //instantiate extension controller, pass standard controller variable into constructor
        coloursController controller = new coloursController (sc);
        
        //executes method within the controller extension which are used for visualforce page
        controller.saveBtn();
        
        
        //executes items getters and setters
        List<String> items = controller.MPItems0;
        controller.MPItems0= items;
       

        //executes options getters
        List<SelectOption> options = controller.MPOptions0;        
       
    }
}


Hope this helps!
Mhlangano KhumaloMhlangano Khumalo
TEST CLASS WITH 100% CODE COVERAGE:- the previous one need test data for the multi-select picklist
@isTest
//class name
private class coloursController_Test{
    //This section covers- 96% coverage 
    private static testMethod void SingleCheckboxChecked(){
        
        Account acc= new Account();            
        acc.Name = 'Account Name';  
        acc.Colour__c = 'Black' ;
        insert acc;
        
        
        //instantiate standardcontroller, pass object variable into constructor
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
       
        //instantiate extension controller, pass standard controller variable into constructor
        coloursController controller = new coloursController (sc);
        
        //executes method within the controller extension which are used for visualforce page
        controller.saveBtn();
                
        //executes items getters and setters
        List<String> items = controller.MPItems0;
        controller.MPItems0= items;
       
        //executes options getters
        List<SelectOption> options = controller.MPOptions0;               
    }
    
    //Multiple selections Section Made- For that 1 line not covered :)
    private static testMethod void MultipleCheckboxesChecked(){
        
        Account acc= new Account();            
        acc.Name = 'Account Name';  
        acc.Colour__c = 'Black;White' ;
        insert acc;
        
        
        //instantiate standardcontroller, pass object variable into constructor
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
       
        //instantiate extension controller, pass standard controller variable into constructor
        coloursController controller = new coloursController (sc);
        
        //executes method within the controller extension which are used for visualforce page
        controller.saveBtn();
                
        //executes items getters and setters
        List<String> items = controller.MPItems0;
        controller.MPItems0= items;
       
        //executes options getters
        List<SelectOption> options = controller.MPOptions0;               
    }    
    
}