Monday 20 January 2014

using multiple values of multiselect picklist field in soql

using multiple values of multiselect picklist field in soql

An example to demonstrate how we can retrieve records with multiple values in multiselect picklist field. Have used a custom multiselect picklist on account Reason__c with values(Reasons 1, Reason 2, Reason 3, Reason 4)In order to retrieve records with multiple values for multiselect field we have to use include/Exclude separate the values by semicolon. For example where include(Reason 1;Reason 2) would return records having both of these two values.



Visualforce page
<apex:page controller="MultiselectController">
  <apex:form >
    <apex:pageblock >
    Accounts having Reasons as 'Reason 1' and 'Reason 2'
      <apex:pageblocktable value="{!accList1}" var="acc1">
        <apex:column value="{!acc1.Name}"/>
        <apex:column value="{!acc1.Reasons__c}"/>
      </apex:pageblocktable> 
      </apex:pageblock>
      <apex:pageblock >       
      Accounts having Reasons as both 'Reason 1' and 'Reason 2' OR only 'Reason 2'
      <apex:pageblocktable value="{!accList2}" var="acc2">
        <apex:column value="{!acc2.Name}"/>
        <apex:column value="{!acc2.Reasons__c}"/>
      </apex:pageblocktable>    
    </apex:pageblock>
  </apex:form>
</apex:page>

Controller
Public class MultiselectController {
    Public List<account> accList1{get;set;}
    Public List<account> accList2{get;set;}
    Public MultiselectController(){
        accList1 = New List<account>();
        accList2 = New List<account>();
        accList1 = [select name,reasons__c from account where reasons__C includes('Reason 1;Reason 2')];
        accList2 = [select name,reasons__c from account where reasons__C includes('Reason 1;Reason 2','Reason 2')];
    }
}

No comments:

Post a Comment