Friday, December 14, 2012

Code snippet for salesforce multiselect picklist

Salesforce Multiselect Picklist

Hi Guys, we generally use multiselect picklist in salesforce here is an example of multiselect picklist.. which allows to show fields based on selected options.

see running example on 

<apex:page controller="MultiselectExample">
  <!-- Begin Default Content REMOVE THIS -->
  <apex:form id="form">
  <div style="border:1px solid gray;width:100%;float:left;">
    <div style="border:1px solid gray;width:50%;float:left;">
        <apex:panelGrid columns="3" id="multipicklistbox">
            <apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:200px" size="8">
                <apex:selectOptions value="{!leftBoxValues}" />
                <apex:panelGroup >
                    <apex:commandButton value=">" action="{!addtoLeftBox}" reRender="form" title="Select Objects"/>
                    <apex:commandButton value="<" action="{!addtoRightBox}" reRender="form" title="Deselect Objects"/>
            <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:200px" size="8">
                <apex:selectOptions value="{!rightBoxValues}" />
         <apex:inputText id="other" label="Other Text" rendered="{!IsOtherOptionSelected}"/>
    <div style="border:1px solid gray;width:48%;float:left;">
    <apex:outputPanel id="selectedItems">
    <apex:commandButton value="Print Selected" reRender="selectedItems"/>

---------- MultiSelectExample.cls------------------

public class MultiselectExample {
    Public List<string> leftSelected{get;set;}
    Public List<string> rightSelected{get;set;}
    Set<string> leftValues = new Set<string>();
    Set<string> rightValues = new Set<string>();
    Set<string> selectedObjects = new Set<string>();
    public MultiselectExample(){
        for(integer i=0;i<10;i++){
            leftValues.add('selectList Item '+i);
        leftValues.add('Other Value');
    /* Add item to rightbox of picklist*/
    public PageReference addtoLeftBox(){
        for(String s : leftSelected){
        return null;
    /* Add item to leftbox of picklist*/
    public PageReference addtoRightBox(){
        for(String s : rightSelected){
        return null;

        /* return rightbox values of picklist */
    public List<SelectOption> getLeftBoxValues(){
        List<SelectOption> options = new List<SelectOption>();
        List<string> tempList = new List<String>();
        for(string s : tempList)
            options.add(new SelectOption(s,s));
        return options;
        /* return leftbox values of picklist */
    public List<SelectOption> getRightBoxValues(){
        List<SelectOption> options1 = new List<SelectOption>();
        List<string> tempList = new List<String>();
        for(String s : tempList)
            options1.add(new SelectOption(s,s));
        return options1;
    public Boolean getIsOtherOptionSelected(){
        Boolean retValue = false;
        for(String selObj: rightValues ){
                return true;
        return retValue;
    public PageReference printSelectedValues(){
        return null;