+ Start a Discussion
Iqra TechIqra Tech 

How to add multiple to select field in dynmaic query

here is my code:-

public with sharing class ObjectQueryController2 {
    public List<Contact> conts{get;set;}
    public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    private Map<String, String> fldAPINameMap = new Map<String,String>();
    //Private set on SelectOption is so that values can be set from the controller.  
    public List<SelectOption> objectNames{public get; private set;}
    public String selectedObject {get; set;}
    public String selectedField {get;set;}
    public String selectedFieldAPI {get;set;}
    private List<SelectOption> fields;
    Public string criteria{get;set;}
    public List<sObject> results{get;set;}
    public boolean queried {get;set;}
    
    // TO DO:
    // - Error handling when user selects ID or Name fields to query (Any Duplicate Fields)
    // - Handle queries of fields that are different than text
    // - Allow the selection of multiple fields
    // - Allow Mass Update of fields on queried records
    // - Insert new records
   
    public List<SelectOption> getObjFields() {        
        fields.clear();
        if(queried == null){queried = false;}
        system.debug('Page loading, Queried is: '+queried);
        
        // If an object was not selected yet, just populate the text 'Select object first'
        if(selectedObject == NULL){
            fields.add(new selectOption('Select Object First', 'Select Object First'));
        } 
                
        // if object was selected, create a list of the object's fields
        else {            
            system.debug('$$$$$' + selectedObject);
            
            //We get a string of the sOjbects using the schemaMap.
            Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
            
            for(Schema.SObjectField sfield : fieldMap.Values())
            {
                schema.describefieldresult dfield = sfield.getDescribe();
                String fieldName = dfield.getLabel();
                fields.add(new SelectOption(fieldName, fieldName));
                fldAPINameMap.put(fieldName, dfield.getname());
            }
        }
        return fields;
    }
    
    // Constructor - this method will run when the controller is instantiated
    public ObjectQueryController2(){
        objectNames = initObjNames();
        fields = new List<SelectOption>();
        List<sObject> results = new List<sObject>(); 
        String Criteria;
    }
    
    // Populate SelectOption list -
    
    // find all sObjects available in the organization
    
    private List<SelectOption> initObjNames() {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
            objNames.add(new SelectOption(name,name));
        return objNames;
    }
    
    //Instantiate the list cast records.  We pull id, Name and other needed fields for our pageBlockTable in our VF page
    
    public void btnPerformQuery(){
        
        string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate,'+fldAPINameMap.get(selectedField)+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' like \'%'+criteria+'%\'';
        selectedFieldAPI = fldAPINameMap.get(selectedField);
        system.Debug(queryStr1);
        results = database.query(queryStr1);
        System.Debug(results.size());
        queried = true;
        system.debug('Query performed, Queried is: '+queried);
        
    }
}
Raj VakatiRaj Vakati
You need to use the IN Clause not like in this case


https://developer.salesforce.com/forums/ForumsMain?id=9062I000000g6KGQAY

 
List<String> criteria = new List<String>() ;
criteria.add('Adipec 2018 ');
criteria.add('Adipec 2017');


string queryStr1 = 'SELECT ID, NAME, createdBy.Name, createdDate, lastModifiedBy.Name, lastModifiedDate,'+fldAPINameMap.get(selectedField)+' FROM '+selectedObject+' WHERE '+fldAPINameMap.get(selectedField)+' IN '+criteria+'';