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
BalkishanBalkishan 

Searching

How can we apply multiple searching using one searching button?

For example we have three text boxes and one searching button.Using that button how can we search using all three criteria.

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Well, instead of using HTML input tags, create a new Account record to facilitate gathering the search criteria, call it "queryAccount".

 

In your Page, replace    <input type="text" name="accountName">   with    <apex:inputField value="{!queryAccount.Name"]/>

and do the same for the city and state fields using BillingCity and BillingState

 

Also, throw and <Apex:Messages /> in there so that you can display error messages.

 

 

So, assuming that AccountController is your Apex Controller class, build a String to hold a dynamically generated query:

 

 

Account queryAccount {get; set;}

 

public pageReference Search() {

     String q = 'select name, phone, BillingCity, Billingstate, BillingCountry from Account where   ';

     String criteria = '';

     if (queryAccount.Name != '') criteria = 'Name = \'' + queryAccount.Name + '\' or ';

     if (queryAccount.BillingCity != '') criteria = criteria + 'BillingCity = \'' + queryAccount.Billingcity + '\' or ';

     if (queryAccount.BillingState != '') criteria = criteria + 'BillingState = \'' + queryAccount.BillingState+ '\' or ';

     if (criteria == '') {

            ApexPages.addMessages(new ApexPages.Message(ApexPages.Severity.FATAL,'No Search Criteria specified');

          return null;

      }

      q = q + criteria  + '  id = null Order by Name'; 

       accountList = Database.Query(q);

      return null;

}

 

In your class constructor:

queryAccount = new Account();

 

 

So, I'm just typing this in by hand so I'm sure there will be things to clean up, try/catch to add, etc., but hopefully this gives you the idea.   I'd also suggest you read the Force.com workbook stuff as it gives a boatload of examples, etc., and this is pretty basic.  

 

Best, Steve.

All Answers

SteveBowerSteveBower

I find your question to be unclear.  Could you elaborate?  Where are these text boxes, buttons, etc.  How are you doing these searches.  What are you searching?  documents, content, objects, etc.

 

In general, if you have one button, and three things to search, and you can't search for all three at the same time, then you have your one button do three searches and return three sets of results.  But, that's so general as to be meaningless, so more clarity from you would help get you a better answer.

 

Best, Steve.

BalkishanBalkishan

<apex:page controller="AccountController">
<apex:form>
<apex:pageBlock>
Name: <input type="text" name="accountName"/>
City: <input type="text" name="city"/>
State: <input type="text" name="state"/>
<apex:commandButton action="{!Search}" value="Search"/>
</apex:pageBlock>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:pageBlockTable value="{!accountList}" var="account">
<apex:column value="{!account.Name}"/>
<apex:column value="{!account.BillingCity}"/>
<apex:column value="{!account.BillingState}"/>
<apex:column value="{!account.BillingCountry}"/>
<apex:column value="{!account.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>>
</apex:form>
</apex:page>

I am using that code to apply searching.I want that i can search records if any of them is filled or all three are filled.

SteveBowerSteveBower

Well, instead of using HTML input tags, create a new Account record to facilitate gathering the search criteria, call it "queryAccount".

 

In your Page, replace    <input type="text" name="accountName">   with    <apex:inputField value="{!queryAccount.Name"]/>

and do the same for the city and state fields using BillingCity and BillingState

 

Also, throw and <Apex:Messages /> in there so that you can display error messages.

 

 

So, assuming that AccountController is your Apex Controller class, build a String to hold a dynamically generated query:

 

 

Account queryAccount {get; set;}

 

public pageReference Search() {

     String q = 'select name, phone, BillingCity, Billingstate, BillingCountry from Account where   ';

     String criteria = '';

     if (queryAccount.Name != '') criteria = 'Name = \'' + queryAccount.Name + '\' or ';

     if (queryAccount.BillingCity != '') criteria = criteria + 'BillingCity = \'' + queryAccount.Billingcity + '\' or ';

     if (queryAccount.BillingState != '') criteria = criteria + 'BillingState = \'' + queryAccount.BillingState+ '\' or ';

     if (criteria == '') {

            ApexPages.addMessages(new ApexPages.Message(ApexPages.Severity.FATAL,'No Search Criteria specified');

          return null;

      }

      q = q + criteria  + '  id = null Order by Name'; 

       accountList = Database.Query(q);

      return null;

}

 

In your class constructor:

queryAccount = new Account();

 

 

So, I'm just typing this in by hand so I'm sure there will be things to clean up, try/catch to add, etc., but hopefully this gives you the idea.   I'd also suggest you read the Force.com workbook stuff as it gives a boatload of examples, etc., and this is pretty basic.  

 

Best, Steve.

This was selected as the best answer
BalkishanBalkishan

I have got solution of my problem through the code you suggest me.I learnt many new things through your code.

Thank you sir.You made me learnt new things in salesforce.

nainanaina

In the statement below, controller refers to what type of controller?

 

 Select the one correct answer. Standard Controller, Custom Controller, Controller Extension ,Specific Controller

 

plz do reply,on 24th nov m goin to gve cerification xam,plzz share some tips and pattern...thanxx