Tuesday, 10 May 2016

Picklist Handling in Salesforce

Hi Awesome Salesforce Developers,

I hope you have enjoyed the last three blogs.

The current blog is related with handling picklist values in salesforce.

We can use picklist in many ways on VisualForce page i.e either use the field defined as picklist data type or build your own picklist using <apex:selectlist>

For this purpose, I have created the custom fields called Product Single Select & Product with datatype as picklist and picklist (Multiselect) on Case object.

On visual force page also, I have created picklists using <apex:selectlist> with attribute multi-select.

The visualforce page will look like below :
















When you select value/values from each picklist and click on show values button the output panel will display how salesforce will give values from each picklist in the apex controller.






















* Please check the result of standard and custom multi-select picklist selection.
In standard multi-select picklist values are separated using (;) and in custom multi-select values are surrounded with [] & (,).

For getting values you need to split the string using (;) or (,).

Sharing below controller and visual force page of above demo :

Apex Controller :
public with sharing class CasePickDemoController
{
    public Case objCase {get;set;}
    public string strProduct {get;set;}
    public string strProducts {get;set;}
    public boolean blnshowresult{get;set;}
    
    public CasePickDemoController(ApexPages.StandardController controller) 
    {
      objCase = new Case();
      blnshowresult = false;
    }
    
    public List<SelectOption> getlstProductSingleSelect()
    {
        List<SelectOption> lstProduct = new List<SelectOption>();
        lstProduct.add(new SelectOption('--None--','--None--'));
        lstProduct.add(new SelectOption('GC1040','GC1040'));
        lstProduct.add(new SelectOption('GC3020','GC3020'));
        return lstProduct;
    } 
     
    public List<SelectOption> getlstProductMultiSelect()
    {
        List<SelectOption> lstProduct = new List<SelectOption>();
        lstProduct.add(new SelectOption('--None--','--None--'));
        lstProduct.add(new SelectOption('GC1040','GC1040'));
        lstProduct.add(new SelectOption('GC3020','GC3020'));
        return lstProduct;
    } 
     
    public void getPickListValues()
    {
      blnshowresult = true;
      system.debug('>'+objCase.Product_Single_Select__c);
      system.debug('>>>>>>>>objCase.Product__c '+objCase.Product__c);
      system.debug('>>>>>>>>strProduct '+strProduct);     
      system.debug('>>>>>>>>strProducts'+strProducts);
    }
}



VisualForce Page :
<apex:page standardController="Case"  extensions="CasePickDemoController">
<apex:form >
<apex:pageblock>
<apex:pageblocksection>
  <apex:pageBlockSectionItem >
 <apex:outputLabel value="Product Single Select Picklist"/> 
 <apex:inputfield value= "{!objCase.Product_Single_Select__c}"/>         
  </apex:pageBlockSectionItem>      
<br/>
   <apex:pageBlockSectionItem >
 <apex:outputLabel value="Product Multi Select Picklist"/>
 <apex:inputField value="{!objCase.Product__c}" />
   </apex:pageBlockSectionItem>          
<br/>
   <apex:pageBlockSectionItem >
 <apex:outputLabel value="Product Custom Single Select Picklist "/>
 <apex:selectList size="5" value="{!strProduct}" multiselect="false">
   <apex:selectOptions value="{!lstProductSingleSelect}" />
 </apex:selectList>
   </apex:pageBlockSectionItem>  
<br/>
   <apex:pageBlockSectionItem >
 <apex:outputLabel value="Product Custom Multi Select Picklist "/>
 <apex:selectList size="5" value="{!strProducts}" multiselect="true">
   <apex:selectOptions value="{!lstProductMultiSelect}" />
      </apex:selectList>
 </apex:pageBlockSectionItem>
   </apex:pageblocksection>
</apex:pageblock>
      
<apex:commandButton value="Show Values" action="{!getPickListValues}"/> <br/>

<apex:outputpanel rendered="{!blnshowresult}">
 <apex:outputLabel value="Values selected from standard pickist field (Single)"/>
 <apex:outputLabel value = "{!objCase.Product_Single_Select__c}"/>
 <br/> <br/>
 <apex:outputLabel 
      value="Values selected from standard pickist field (MultiSelect)"/>
 <apex:outputLabel value = "{!objCase.Product__c}"/> 
 <br/> <br/>
 <apex:outputLabel 
     value="Values selected from custom pickist (Multiselect= false)"/>
 <apex:outputLabel value = "{!strProduct}"/>
  <br/> <br/>
 <apex:outputLabel 
    value="Values selected from custom pickist (Multiselect= true)"/>
 <apex:outputLabel value = "{!strProducts}"/>   
</apex:outputpanel>
</apex:form>
</apex:page>

Happy Coding !!!!