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
ASMASM 

Search in VF having four search fields

Hi,
    I am creating a Search Page using VF. I have four input search fields and would like to display results in a table. I have created search pages before using only two input fields. It worked fine but I am not able to do the same with four search fields. Could anyone help me with this please ?  I am using if statements to get the results. Could anyone could tell me a better way of doing a search ?
 
Thanks
hisrinuhisrinu
Hi,

  I developed the search using 4 inputtext fields it is working fine for me.
 Please check once your code, may be is there any problem or else you can post some sample data here.
ASMASM
Did you use if else statements to do the search ? This is my Controller code:
Code:
public class adsController {
    public String adString;
    public String companyString;
    public String sizes;
    public String style;
    
    public void setAdString(String val1){
        adString=val1;
    }
    public String getAdString(){
        return adString;
    }
    public void setCompanyString(String val2){
        companyString=val2;
    }
    public String getCompanyString(){
        return companyString;
    }
    public void setSizes(String sizes){
        this.sizes=sizes;
    }
    public String getSizes(){
        return sizes;
    }
    public void setStyle(String style){
        this.style=style;
    }
    public String getStyle(){
        return style;
    }
    public List<Ads__c> getAds(){  

if (adString!='' && companyString!='' && sizes!='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND Account__r.name LIKE :companyString+'%' AND size__c=:sizes AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString!='' && sizes!='' && style=='') 
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND Account__r.name LIKE :companyString+'%' AND size__c=:sizes ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString!='' && sizes=='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND Account__r.name LIKE :companyString+'%' AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString=='' && sizes!='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND size__c=:sizes AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString=='' && companyString!='' && sizes!='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE Account__r.name LIKE :companyString+'%' AND size__c=:sizes AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString!='' && sizes=='' && style=='') 
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND Account__r.name LIKE :companyString+'%' ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString=='' && sizes=='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString=='' && companyString=='' && sizes!='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE size__c=:sizes AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString=='' && sizes!='' && style=='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' AND size__c=:sizes ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString=='' && companyString!='' && sizes=='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE Account__r.name LIKE :companyString+'%' AND style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString=='' && companyString!='' && sizes!='' &&style=='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE Account__r.name LIKE :companyString+'%' AND size__c=:sizes ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString=='' && companyString=='' && sizes=='' && style!='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE style__c=:style ORDER BY LastModifiedDate LIMIT 120];
}
else if(adString!='' && companyString=='' && sizes=='' && style=='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE name LIKE :adString+'%' ORDER BY LastModifiedDate LIMIT 120];
}   
else if(adString=='' && companyString!='' && sizes=='' && style=='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE Account__r.name LIKE :companyString+'%' ORDER BY LastModifiedDate LIMIT 120];
} 
else if(adString=='' && companyString=='' && sizes!='' && style=='')
{ return [SELECT Name, Account__r.name,size__c,style__c FROM Ads__c WHERE size__c=:sizes ORDER BY LastModifiedDate LIMIT 120];
} 
else  
{ return NULL;
} 
}   
    public PageReference searchAd() {
        return Page.searchAdRef2;
    }

}

 I know this is very bad coding technique but I do not know if there is a better way to do this using VF. It would be great if you can suggest a better method to implement Search. Because if I want to increase the input Txt fields, it would lot more if's which would be impossible to code. thank you
kyleRochekyleRoche
I'm not sure you could do it much better w/out Dynamic SOQL enabled in your org. I'm looking to build the same sort of thing though. Has anyone tried getting dynamic SOQL enabled? What was the process?
gireeshzgireeshz

I actually just treid getting dynamic SOQL enabled and this is the answer I received:

 

"We are running a Limited Release on this, and are accepting people via nomination process.  We are only able to add this to your Sandbox at this time and it cannot be included in a managed package or distributed as part of a partner application.  This may change in the future.

 Please confirm that you would like to be submitted into this program and I will fill out the form."

 

also, got this information:

 

"The plan is to make this a wide release, so I will go ahead and add you to the nomination list.  Expect contact from someone about this request to further qualify your inclusion.

 

The only caveat is that customers must have 50 licenses "

 

Even if it gets enabled, they will only do it for a Sandbox environment until it goes GA, which I have no idea about when that is.   I have decided to abandon it for now and do things the inefficient way...

ASMASM
Thanks for replying
hisrinuhisrinu
Code:
public List<SelectOption> getBillto_SearchList_Fields()
    {

        List<SelectOption> options = new List<SelectOption>();
        for(SMA_Country__c c : countries)
        options.add(new SelectOption(c.Name,c.Name));
        return options;
        
    }
    String Billto_List_Value;
    public String getBillto_Selected_Field()
    {
        return Billto_List_Value;
    }
    public void setBillto_Selected_Field(String Billto_List_Value)
    {
        this.Billto_List_Value=Billto_List_Value;
    }
    String Billto_Search_Keyword;
    public void setBillto_Keyword(String s)
    {
        Billto_Search_Keyword = s;
    }
    public String getBillto_Keyword() 
    {
        return Billto_Search_Keyword;
    }
    String BCNameKeyword;
    public String getBCNameKeyword() 
    {
        return BCNameKeyword;
    }
    public void setBCNameKeyword(String s)
    {
        if(s=='')
            BCNameKeyword='%';
        else
            BCNameKeyword = s+'%';
    }
    String BCNumKeyword;
    public String getBCNumKeyword() 
    {
        return BCNumKeyword;
    }
    public void setBCNumKeyword(String s)
    {
       if(s=='')
               BCNumKeyword = '%';
      else  
        BCNumKeyword = s+'%';
    }
    String BCCustypeKeyword;
    public String getBCCustypeKeyword() 
    {
        return BCCustypeKeyword;
    }
    public void setBCCustypeKeyword(String s)
    {
    if(s=='')
        BCCustypeKeyword='%';
    else
        BCCustypeKeyword = s+'%';
    }

public pageReference Billto_Customers()
{
       
            Billto_Customer_Records=[select Bill_To_Number__c,Name,parent.name,Corp_code__c,Cust_Type__c,BillingCountry,BillingStreet,BillingCity,BillingState from Account where BillingCountry =: Billto_List_Value and Name like :BCNameKeyword and Bill_To_Number__c like :BCNumKeyword and Cust_Type__c like :BCCustypeKeyword and Bill_To_Number__c !=null];
}

This is the sample code I used. Its working fine for me
 
ASMASM

Hi there,

    I have tried to use your code and I am not able to generate the search functionality. It does not give result if I search using only one field.

 

ASMASM

I am actually trying to build a advanced search page with 8 fields in it. I do not know of a better way of doing it using the VF than the method

which I have posted above, which has simple if statements to check all the possiblities of fields. I would like to know a better way to do this.

Thanks,

Anusha