function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raj88Raj88 

How to filter records using multiselect picklist values?

I need to fetch records based on multiple pickl;ist value i have selected. 
This is my VF page.


       <apex:pageBlock> 
            <apex:pageBlockSection >
                <apex:outputLabel> Account Filter: &nbsp;
                    <apex:selectList value="{!selectedList}" size="5" id="AccountFilter" multiselect="true">
                        <apex:selectOptions value="{!selectList1}"/>
                        <apex:actionSupport action="{!loadDetails}" event="onclick" reRender="AccDMLTable"/>    
                    </apex:selectList>
                </apex:outputLabel> 
            </apex:pageBlockSection>              
            <apex:pageBlockSection > 
                <apex:outputLabel > Status Filter: &nbsp;
                    <apex:selectList value="{!selectedstList}" id="StatusFilter" size="5" multiselect="true">
                        <apex:selectOptions value="{!selectList2}"/>
                        <apex:actionSupport action="{!loadDetails}" event="onclick" reRender="AccDMLTable"/>
                    </apex:selectList> 
                </apex:outputLabel>    
            </apex:pageBlockSection>
      </apex:pageBlock>
    <apex:pageBlock>
        <table class="display" border= "1" id="AccDMLTable">
         <apex:repeat value="{!OppList}" var="a" id="AccDMLTable">
         <tr>
          <td>{!a.acc.Name}</td>
            <td>{!a.acc.Status}</td>
            <<td>{!a.acc.LastModifiedDate}</td>
         </tr>
         </apex:repeat>
         </table>
        <apex:commandButton value="Save" action="{!SaveAcc}" reRender="AccDMLTable"/>     
    </apex:pageBlock>
Keyur  ModiKeyur Modi
Hi,
You can  do dynamic query like .

string query ='SELECT id,name FROM opportunity WHERE stage IN ('closed Won','closed Lost')'
Database.Query(query);

same way you ca take your picklist value and you can set that value in the query.
In your case you can do like this.

string []slectedPicklistValue=selectedstList.split(','); // here selectedstList will be the value which you will get after selecting the picklist value
string allSelectedPicklist=''";

for (string eachPickValue:slectedPicklistValue){

allSelectedPicklist= ''\' + eachPickValue +''\' +',' ;  // in this variable "allSelectedPicklist" , there is all multipicklist value.
}
string query = 'SELECT id,name FROM account WERE status IN (' +allSelectedPicklist +')' ;
Database.query(query);

Thanks,
Keyur Modi
 
Raj88Raj88
Thanks Keyur for reply.
But i am getting this error when i select the picklist values.

unexpected token: '['
Error is in expression '{!SaveAcc}' in page testopp: Class.OppDML.SaveAcc: line 135, column 1


This s the code i have updated:
      string [] selectList1 =selectedstList.split(','); 
      string allselectList ='';
      for (string eachPickValue :selectList1 ){
      allselectList= eachPickValue; 
      }
      string query = 'select id, name Where Name IN ('+allselectList+')';
      Database.query(query);
Keyur  ModiKeyur Modi
Hi,
why you are getting this error because you need to put selected picklist value in single quote .. you can refer abow and where i shared sample query on oppportunity .


string query ='SELECT id,name FROM opportunity WHERE stage IN ('closed Won','closed Lost')'

here in this example if you see closed won and closed lost both picklist value are quoted . 

please let me know if this will help.

Thanks,
Keyur Modi
Raj88Raj88
Getiing error while trying to save the apex class.
Compile Error: expecting a semi-colon, found 'Closed' at line 134 column 387

Thanks
Vivekh