+ Start a Discussion
chaitanya salesforcecrmchaitanya salesforcecrm 

Kindly let me know how to correct my code.

I have written a Controller for searching data on visualforce page. below is the controller.
public with sharing class Condosearchcontroller { 
   public Condosearchcontroller() {

    }

    public list <Condo__c> Cond {get;set;}  
    public string beds {get;set;} 
    public string baths {get;set;}
    public string squarefootage {get;set;} 
    public Condosearchcontroller(ApexPages.StandardController controller) {  
   }  
   
   public void search(){
   if (squarefootage == null){
    string searchquery='select name,Beds__c,Baths__c,Square_Footage__c from Condo__c where Beds__c = '+beds+' and Baths__c = '+baths+''; 
        Cond = Database.query(searchquery);
      }
      else { 
      string searchquery1='select name,Beds__c,Baths__c,Square_Footage__c from Condo__c where Beds__c = '+beds+' and Baths__c = '+baths+'and Square_Footage__c = '+squarefootage+'';
      Cond = Database.query(searchquery1);
}
}

when i enter data in all the 3 fields beds,baths,square footage. i am getting results.
but when i enter only beds,baths in visual force page.then i am getting error.
Best Answer chosen by chaitanya salesforcecrm
venkat-Dvenkat-D
You can do something like this. when you are building your query.
1) you can check for each field value and then append.
if(bes__c != null){
searchquery = ' basic query ending with where' + ' beds__c ='+beds ;
}
similarly check for all. 
2) You can use OR so that you will get results taht matches with atleast one.