+ Start a Discussion
Board salesforceBoard salesforce 

where clause for the dynamic search in fieldsets

Hi all,
can any one help on this .
 
public pageReference searchProducts(){
        Map<String,Object> fieldValuesMap =new Map<String,Object> ();
        
        String query = 'select id'; 
        System.debug('query first ------>'+query );
        string WhereClause;
        for(Schema.FieldSetMember f : SObjectType.Product2.FieldSets.SearchProducts.getFields()) 
        {
            query += ', ' + f.getFieldPath();  
            WhereClause=''+schema.product2.getSobjectType()+' LIKE \'%'+ f.getFieldPath() + '%\'  ';
            System.debug('WhereClause ------>'+WhereClause );
        }
        query += ' FROM Product2 WHERE ' +WhereClause;
        
        System.debug('query ------>'+query );
        Products = Database.query(query);
        System.debug('Products ------>'+Products );
         
        for(Schema.FieldSetMember f : SObjectType.Product2.FieldSets.ProductFields.getFields()) 
        {
            
            WhereClause='id=:Product2.id ,Product2.'+f.getfieldPath()+' LIKE \'%'+ f.getFieldPath() + '%\'  ';
            System.debug('WhereClause ------>'+WhereClause );
        }
        WhereClause += ' FROM Product2 WHERE ' +WhereClause; 
        
        System.debug('query ------>'+query );
        Products = Database.query(query);
        System.debug('Products $$$$$ ------>'+Products );
        query=Query +'From Product2'+WhereClause;
        System.debug('query $$$$$ ------>'+query );
        return Null;
    }

in this method am not able to get the product values in search functionality
 
Best Answer chosen by Board salesforce
Dayakar.DDayakar.D
Hi,
Try below code it will help you,
String queryString='select id';
        //adding select columns from Search Result fielsSet
        for(Schema.FieldSetMember fld :SObjectType.Product2.FieldSets.Search_Result.getFields()) {
            queryString += ', ' + fld.getFieldPath();
        }
        queryString=queryString+' from Product2';
        String whereClause=' where IsActive=true';
        //Building where clause using Search Filter FieldsSet
        for(Schema.FieldSetMember fldset :SObjectType.Product2.FieldSets.Search_Filter.getFields())
        {
            if((String)productSearch.get(fldset.getFieldPath())!=null)
            {
                whereClause=whereclause+' and '+fldset.getFieldPath()+' Like \'%'+(string)productSearch.get(fldset.getFieldPath())+'%\' ';
            }
        }
        string query=queryString+whereClause;
        system.debug('query'+query);
        ProductsList=Database.query(query);
//Here productSearch is property in wich products are stored
Please let me know, if you have any question,

BestRegards,
Dayakar.D