I got the following Apex and VF code doing a string search on Name field. There are 3 other fields that I have to use to do the search. Ex: Name, Address, Phone, Email are the fields and the user should be able to enter some text on each text field and the records should be displayed.

My question is how can I do the search for other fields as well, can I use the same String Variable on different queries ?

Apex Code----------

 public with sharing class Search{

        public List<Entity__c> entity {get; private set;}
        public String searchString {get; set;}

      public void stringSearchValue(){
            string searchquery = 'SELECT Name, Address__c, Phone__c, Mail__c FROM Entity__c WHERE Name LIKE \'%'+searchString+'%\' AND Mail__c =\'KBS\' LIMIT 20';
            entity = Database.query(searchquery);



<apex:pageBlock title="String Search">

                        <apex:pageBlockSection collapsible="true" columns="1">

                            <apex:inputText label="Name" value="{!searchstring}" />

                            <apex:commandButton value="Search" action="{!stringSearchValue}"/>

                                    <apex:pageBlockTable value="{!entity}" var="ent">
                                        <apex:column headerValue="Name" value="{!ent.Name}"/>
                                        <apex:column headerValue="Address" value="{!ent.Addresse__c}"/>
                                        <apex:column headerValue="Phone" value="{!ent.Phone__c}"/>
                                        <apex:column headerValue="Mail" value="{!ent.Mail__c}"/>

                            <apex:commandButton value="Cancel" action="{!stringSearchCancelPopUp}" rerender="searchPopup" immediate="false"/>


3 Answers 3


I would use SoSL for something like that. SoSL is the technology used for Search.

You could do something like this:

string searchquery = 'FIND \'*' + searchString+ '*\' IN ALL FIELDS RETURNING Contact (Name, Address__c, Phone__c, Mail__c) LIMIT 20';

entity = search.query(searchquery);


  • SOSL is worth using if we are doing a search in differant objects right ? but in this case i'm doing a record search using fields on the same object. Have you got any idea how I can do this ?
    – Tinkerbell
    Nov 13, 2015 at 10:53
  • 1
    Not really, The sample I left you is doing a search in different fields on one object only. The trick there is that you don't have that infinite control on which fields are being searched, though you could control that the same way you control which fields are available for chatter searches Nov 16, 2015 at 21:11

The answer to this really depends on your requirements.

Are you trying to allow users to enter a single parameter and find all accounts where Name, Address, Phone, or Email matches the parameter? If so, the SOSL approach in the other post is great.


Are you trying to allow users to drill down to find Accounts by entering specific (and separate) parameters for Name, Address, Phone, and Email? If so, you will want unique String inputs for each parameter and you will want to add all non-blank parameters to your Account SOQL query.


Adding two other variables as a searchString for other fields and using a OR statement within WHERE clause fixed the issue. I referenced the variables for each searchTexts in the VF page.

string searchquery = 'SELECT Name, Address__c, Phone__c, Mail__c FROM Entity__c WHERE (Name LIKE \'%'+searchString+'%\' OR Address__c LIKE \'%'+searchString2+'%\' OR Phone__c LIKE \'%'+searchString3+'%\') AND Mail__c =\'KBS\' LIMIT 20';
            entity = Database.query(searchquery);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .