0

I have created a simple vf page with 1 single select picklist(Application) and 1 multiselect picklist(Application Status) ; code below.

<div class="form-group">
    <div class="col-sm-6">
        <label for="registration-no">Application: </label>
    </div>
    <div class="col-md-6">
        <apex:selectList value="{!selectedApplication}" multiselect="false" size="1">
            <apex:selectOption itemValue="None" itemLabel="--None--"/>
            <apex:selectOption itemValue="Application_1_Lead_Status__c" itemLabel="Application 1"/>
            <apex:selectOption itemValue="Application_2_Lead_Status__c" itemLabel="Application 2"/>
            <apex:selectOption itemValue="Application_3_Lead_Status__c" itemLabel="Application 3"/>
        </apex:selectList>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-6">
        <label for="registration-no">Application Status: </label>
    </div>
    <div class="col-md-6">
        <apex:selectList value="{!ApplicationStatus}" multiselect="true" size="3">
            <apex:selectOption itemValue="None" itemLabel="--None--"/>
            <apex:selectOption itemValue="New" itemLabel="New"/>
            <apex:selectOption itemValue="Working" itemLabel="Working"/>
            <apex:selectOption itemValue="Switch off / Phone Ringing" itemLabel="Switch off / Phone Ringing"/>
            <apex:selectOption itemValue="Not Eligible" itemLabel="Not Eligible"/>
        </apex:selectList>
    </div>
</div>         

Now in its class i want to write a SOQL which will give list of leads where selectedApplication(output of first picklist) IN ApplicationStatus(Multiselect picklist)

eg if i select "Application 1" in first picklist and "New;Working" these 2 values in 2nd picklist then that query should work like:-

Select id from Lead where Application_1_Lead_Status__c = "New" 
    OR Application_1_Lead_Status__c = "Working" ;

Also all 3 fields (Application_1_Lead_Status__c,Application_2_Lead_Status__c,Application_3_Lead_Status__c) are single select picklists.

I was hoping ...something like this would work :

Select id from Lead where selectedApplication In ApplicationStatus

but ApplicationStatus will have value something like New;Working which will not work with In clause of SOQL.

  • Please edit your question and add what have you tried so far? – Raul Sep 29 '17 at 10:44
  • I have added an example of what i have tried – Dnyaneshwar Sep 29 '17 at 10:57
  • why just not split the values(so that can be used with IN) and use dynamic query to make your own string ? – Avidev9 Sep 29 '17 at 11:15
  • Yeah that could work...Let me try that – Dnyaneshwar Sep 29 '17 at 11:20
3

So dynamic query is way to go.

Create a method like this may be ?

public static List<Lead> query(String field, String values){
    List<String> statuses = values.split(';');
    return Database.query('SELECT Id, FROM Lead WHERE '+field+' IN:statuses');

}

And you can call the same from your button action or wherever you want from

List<Lead> leads = query(selectedApplication, ApplicationStatus);
  • Thanks for answer...but query is not returning proper results. In my console i ran a query :- SELECT Id FROM Lead WHERE Application_1_Lead_Status__c IN ('New','Working') which returned me 20 records but query generated from method :- SELECT Id FROM Lead WHERE Application_1_Lead_Status__c IN:statuses is returning 0 records. i used debug log to see value of statuses variable and it was : [New, Working] – Dnyaneshwar Sep 30 '17 at 7:33
  • I tried doing this my dev seems to be working, make sure right variables are passed along – Avidev9 Sep 30 '17 at 8:13
  • Yes i am using all variable correctly....thats why the output of above method is coming as : SELECT Id FROM Lead WHERE Application_1_Lead_Status__c IN:statuses which is correct but i also printed values of "statuses" & they were shown like [New, Working] which i think is wrong – Dnyaneshwar Sep 30 '17 at 14:41
  • I passed a list of string:- String[] statuses22 = new String[]{'New','Working'}; directly in code and modify query to use statuses22 instead of statuses. And result was proper. Could it problem of square bracket? becoz when i printed these values using debug statement statuses22 was = (New, Working) and statuses was = [New, Working] – Dnyaneshwar Sep 30 '17 at 15:03
  • Totally depends on code how its implemented if ApplicationStatus is a list of string, I guess you need to split it and directly use in the method, if its semicolon seperated string, you need to split it. – Avidev9 Oct 1 '17 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.