You need to sign in to do that
Don't have an account?

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:
<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:
<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>
This is my VF page.
<apex:pageBlock>
<apex:pageBlockSection >
<apex:outputLabel> Account Filter:
<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:
<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>
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
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);
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
Compile Error: expecting a semi-colon, found 'Closed' at line 134 column 387
Thanks
Vivekh