Wednesday, 22 October 2014

Displaying a picklist in Mulit Select Checkbox format in Salesforce

Hi All,

Generally we provide flexibility to user so that user can select multiple value form a picklist at a time. This post is only for displaying a pick-list in multi select check-box format.  

Here is the page:-

<apex:page controller="multiselectCheckboxController ">
    <style>
    input[type='checkbox'] {
        border: 1px solid #aaa;
        display: block;
    }
    </style>
   <apex:form> 
        <apex:pageBlock>
        <apex:pageBlockSection columns="1">
            <apex:pageblocksectionitem >          
                <apex:outputlabel value="Industory" />
                <apex:selectcheckboxes layout="pageDirection" value="{!Industoryname}">                   
                    <apex:selectoptions value="{!Industorynames}" />          
                </apex:selectcheckboxes> 
            </apex:pageblocksectionitem>
     </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

</apex:page>

Controller :--

public class multiselectCheckboxController {

    public List<string> Industoryname{get;set;}

public multiselectCheckboxController () 
    {
        Industoryname =new List<String>();
    }

public List<selectoption> getIndustorynames()
    {           
        list<selectoption> options = new list<selectoption>();            
        try 
        {               
        //Product Name is a MultiSelect Picklist               
        Schema.DescribeFieldResult fieldResult = Account.Industry.getDescribe();

        list<schema.picklistentry> values = fieldResult.getPickListValues();               
        for (Schema.PicklistEntry a : values) 
        {                  
        options.add(new SelectOption(a.getLabel(), a.getValue()));
        }           
        }  
        catch (Exception e) 
        {             
        ApexPages.addMessages(e);           
        }
        system.debug('## Product Name Options'+ options);          
        return options; 
    }

}