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
mukesh khandve786mukesh khandve786 

how to create filter logic for dyanmic Qurey

Ramesh DRamesh D
Try something like this 
//Add your query fields to List
List<String> fields = new List<String>{
        'Id',
        'Id',
        'Name'  
    };
//Construct query
    String soql = 'SELECT ' + String.join(fields, ',') +
        ' FROM customObject__c' +
        ' WHERE Enrollment_Status__c IN (\'Matriculated\', \'Admitted\')';

    // Search String query builder
    if (String.isNotEmpty(termFilter)) {
      soql += ' AND Id = \'' + termFilter + '\'';
    }
    // Search String query builder
    if (String.isNotEmpty(termCampusFilter)) {
      soql += ' AND Name = \'' + termCampusFilter + '\'';
    }   

    soql += ' ORDER BY motivis_lrm__Student__r.Name';

   system.debug('>>>>>>>>>>>>>>>'+soql);
    studentsList = Database.query(soql);

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
 
Raj VakatiRaj Vakati
You can do it like this
 
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();
    }
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Mukesh,

Please check out the given link :

-http://www.salesforceben.com/salesforce-reports-url-hack/

-http://www.tgerm.com/2011/04/passing-params-to-reports-salesforce.html

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha