Monday, January 28, 2013

Display multi-select picklist as multi-select checkboxes

Use Case :- To improve user experience, we can implement multi-select picklist as multi-select checkboxes on a page.


Steps to accomplish this :-
1.     Create a Page and a extensions for the page. Can't use custom controller assuming that we need to override new button of the custom object.
2.     Extensions Code as below:-

public class newcustomobjectextension {
    public CustomObject pr{get; set;}
     //initialize
    public newproductrequestcontroller(ApexPages.StandardController controller){
      pr=new CustomObject();
    }
   
    //get the multi-select pick list values
    public List<SelectOption> MPOptions {
     get {
       List<SelectOption> options = new List<SelectOption>();
       for( Schema.PicklistEntry f : CustomObject.CustomMultiSelectPicklist.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.CustomMultiSelectPicklist!=null && this.pr.CustomMultiSelectPicklist.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.CustomMultiSelectPicklist= selectedConcat;
     }
   } 
   
   //create record
   public PageReference savepr() {
        insert pr;
        return (new PageReference('/'+pr.id));
   }
}

3.Page code as below:-

<apex:page standardController="CustomObject" extensions="newcustomobjectextension" id="prpage">
 <apex:form id="prform">
 <apex:pageBlock id="prblock">
   <apex:pageBlockSection title="Purpose of Request" id="prmain"><br/><br/><br/>
    <font color="red"><b><i>How will this software be used (select the most relevant)?</i></b></font><br/>
    <center>
     <apex:selectCheckboxes value="{!MPItems}" layout="pageDirection" required="true" id="prodreq">
       <apex:selectOptions value="{!MPOptions}"/>
     </apex:selectCheckboxes>
    </center>
   </apex:pageBlockSection>
   <apex:pageblockButtons location="bottom">
    <apex:commandButton action="{!savepr}" value="Create Request"/>
    <apex:commandButton action="{!cancel}" value="Cancel"/>
   </apex:pageblockButtons>
  </apex:pageBlock>
 </apex:form>  
</apex:page> 

4.Now override the new button of the custom object with this page.













4 comments:

  1. Hi,
    Actually I Displayed all fields Dynamically without using fieldset dynamically in an visualforce page.In my page more than one multipicklists are there.How to visible these multipicklists picklist values as checkboxes dynamically?.

    please help me..........

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi Sir,
    I want above code Everytime comming multiselectpicklist how?

    help me..........

    ReplyDelete
  4. I've tried this and it never saves anything. Is there a way to get the checkboxes to save tie the Subject (Object__c.Field__c) that the page is currently using with it's standard controller?

    ReplyDelete