6

i am developing a search functionality in Vf page. I have a 5 filters on the search.

I need to be able to allow filter logic like in reports

(1 AND 2) OR 3

i am not sure of the logic i need to use to build the query. If i accept the filter logic in a variable and have this on my controller. How do i parse this string and build the query

for eg

filter 1 = industry equals 'Agriculture'

filter 2 = regions equals 'Asia'

filter 3 = partner equals true

and filter logic = (1 AND 2) OR 3

how do i parse this filter logic and build the query accordingly

6
  • Are you developing a SOQL search? Apr 30 '13 at 10:32
  • yes a SOQL search
    – Prady
    Apr 30 '13 at 10:33
  • Based on user input but with your own filters? Apr 30 '13 at 10:33
  • idea is to allow user to choose the logic, if he wants to use and or between the filters. The user chooses the logic he would enter (1 AND 2) OR 3
    – Prady
    Apr 30 '13 at 10:38
  • Dou you want to allow unlimited or limited number of combinations? Apr 30 '13 at 10:51
7

I imagine it like this. You define your logic tests as strings, put them in a map in the controller and create a parser function.

Controller:

public with sharing class test1 {

    public String userLogic { get; set; }
    public String output { get; set; }
    public String filter1 { get; set; }
    public String filter2 { get; set; }
    public String filter3 { get; set; }
    Map<Integer, String> filterMap = new Map<Integer, String>();

    public void createLogic(){

        filter1 = 'Industry__c = \'Agriculture\'';
        filter2 = 'Region_c = \'Asia\'';
        filter3 = 'Partner__c = true';

        filterMap.put(1, filter1);
        filterMap.put(2, filter2);
        filterMap.put(3, filter3);
    }

    public PageReference parseLogic(){

        if(String.isNotBlank(userLogic)){
            String query = 'SELECT Id, Name FROM MyObject__c ';    
            output = userLogic;
            for(Integer i = 1; i <= filterMap.size(); i++){
                if(output.contains(String.valueOf(i))){
                    output = output.replace(String.valueOf(i),filterMap.get(i));
                }
            }
            query += ' WHERE (' + output + ') ';
            query += ' LIMIT 100';

            output = query;
        }

        return null;
    }

    public test1(){
        createLogic();
    }
}

Page:

    1. <apex:outputText value="{!filter1}" /> <br/>
    2. <apex:outputText value="{!filter2}" /> <br/>
    3. <apex:outputText value="{!filter3}" /> <br/><br/>

    Enter your logic: 
    <apex:inputText value="{!userLogic}"/>
    <apex:commandButton action="{!parseLogic}" value="Parse" reRender="outputString"/>

    <br/><br/>

    Output: <apex:outputText value="{!output}" id="outputString" style="font-weight:bold;"/>

Result:

enter image description here

4
  • Wow!!! Thats a neat piece of code...
    – Prady
    Apr 30 '13 at 11:46
  • 1
    Good piece of code, good job. Apr 30 '13 at 11:51
  • It is a very helpful code. But what would you do to ensure the userlogic inputted is in correct format.
    – user2899
    May 22 '13 at 21:39
  • But what if the filters cannot be hardcoded in the code and have to be taken from the condition object? how do we do that? Jan 31 '16 at 7:58

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