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 http://ajayhada.force.com/MultiSelectTest 

---------- MultiSelectExample.page------------------
<apex:page controller="MultiselectExample">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  <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:selectList>
                <apex:panelGroup >
                    <br/>
                    <apex:commandButton value=">" action="{!addtoLeftBox}" reRender="form" title="Select Objects"/>
                    <br/><br/>
                    <apex:commandButton value="<" action="{!addtoRightBox}" reRender="form" title="Deselect Objects"/>
                </apex:panelGroup>
            <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:200px" size="8">
                <apex:selectOptions value="{!rightBoxValues}" />
            </apex:selectList>
        </apex:panelGrid><br/>
         <apex:inputText id="other" label="Other Text" rendered="{!IsOtherOptionSelected}"/>
    </div>
    <div style="border:1px solid gray;width:48%;float:left;">
    <apex:outputPanel id="selectedItems">
        {!rightBoxValues}
    </apex:outputPanel>  
    </div>
  </div>
    <apex:commandButton value="Print Selected" reRender="selectedItems"/>
    </apex:form>
</apex:page>

---------- 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(){
        rightSelected.clear();
        for(String s : leftSelected){
            leftValues.remove(s);
            rightValues.add(s);
        }
        return null;
    }
    
    /* Add item to leftbox of picklist*/
    public PageReference addtoRightBox(){
        leftSelected.clear();
        for(String s : rightSelected){
            rightValues.remove(s);
            leftValues.add(s);
        }
        return null;
    }

        /* return rightbox values of picklist */
    public List<SelectOption> getLeftBoxValues(){
        List<SelectOption> options = new List<SelectOption>();
        List<string> tempList = new List<String>();
        tempList.addAll(leftValues);
        tempList.sort();
        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>();
        tempList.addAll(rightValues);
        tempList.sort();
        for(String s : tempList)
            options1.add(new SelectOption(s,s));
        return options1;
    }
    
    public Boolean getIsOtherOptionSelected(){
        Boolean retValue = false;
        for(String selObj: rightValues ){
            if(selObj.contains('Other')){
                return true;
            }
        }
        return retValue;
    }
    
    public PageReference printSelectedValues(){
        return null;
    }
}