In Salesforce, searching for records is a common task for users. However, the standard search and list view filters have limitations when searching across multiple fields at once. This is where a custom search page becomes very useful.
Recently, our company planned to conduct a Salesforce event. For that, we created an Event Registration form and published it on a public site. We received a large number of registrations, which we stored in a custom object named Event_Registration__c.
Now, in the object’s list view, when we search for records, the search only allows us to search specific fields and doesn’t support combining multiple filters.
To solve this, I decided to create a custom search page using a Visualforce Page and an Apex Controller. On this page, we can select specific field values and search for records across multiple fields simultaneously.
In this article, we will learn how to create a custom search page in Salesforce with Visualforce and Apex Controller. I will explain how a custom search page can make record searching easier than using the standard list view search.
What is a Custom Search Page in Salesforce?
A custom search page is a user interface that allows users to search for records using multiple input fields. It is built using Visualforce for UI and Apex for logic.
Normally, Salesforce list views allow filtering based on limited conditions. But with a custom search page, you can:
- Search using multiple fields
- Combine filters dynamically
- Display results instantly
This makes the search process faster and more flexible.
What is Visualforce in Salesforce?
Visualforce is a markup language used to design custom user interfaces in Salesforce. It works with Apex controllers to fetch and display data.
A Visualforce page mainly contains:
- UI components (input fields, buttons, tables)
- Controller connection (Apex class)
- Data binding between UI and Apex
Visualforce follows the MVC pattern, where:
- Model → Data (Objects)
- View → Visualforce Page
- Controller → Apex Class
What is Apex Controller?
An Apex controller is a class that contains the logic for the Visualforce page.
It is responsible for:
- Fetching data from Salesforce
- Applying filters
- Returning results to UI
There are two types:
- Standard Controller
- Custom Controller
In our case, we use a Custom Controller to implement dynamic search logic.
Create a Custom Search Page in Salesforce [Visualforce Page & Apex]
Below, I will explain how to create a custom search page in Salesforce using a Visualforce Page and an Apex Controller.
We will see how it allows us to search for records across multiple fields at once, making it easier to find exactly what we are looking for without scrolling through long lists of records.
In the first image, you can see that we searched for records by Event Name, and the results were displayed based on the value we entered in the search box.
In the second image, we searched for records using the Registration Status field. However, in the standard list view, you can see that we cannot search for multiple criteria simultaneously.
This means if we want to filter by both Event Name and Status, we have to create a separate list view or run two separate searches.

Apex Class: Retrieve Data and Search Logic in Salesforce
Now, let’s create the Apex class where we will write the logic to search for records based on the values entered in the search fields.
This class retrieves data from the Event_Registration__c object and returns it to our Visualforce page, enabling us to display the matching results instantly.
Here, we declared variables with get and set methods that the VF page uses to fetch values from this controller and send updated values back.
Next, we will define a list collection to store the registration records and build a SOQL query dynamically based on the user-entered search filters (name, event, session, status, registration date).
Then, it runs the query and stores the matching records in the registrations list. To retrieve the records from the Event Registration object, we will use the SOQL query.
SELECT Name__c, Email__c, Phone__c, Registration_Date__c, ' +
'Event_Name__c, Session__c, Status__c ' +
'FROM Event_Registration__c WHERE Id != null'The above query is just a placeholder so we can easily add more AND conditions later, without worrying about where to put the first AND. For each filter, we check if the user entered something, and if yes, we append (+=) the condition of the query.
if (!String.isBlank(searchName)) {
query += ' AND Name__c LIKE \'%' + String.escapeSingleQuotes(searchName) + '%\'';
}- Checks if searchName is not empty.
- Uses LIKE with
%so partial matches are allowed. - String.escapeSingleQuotes() prevents errors if the input has
'(single quote).
This approach allows us to check all fields using an if condition, enabling the user to search for records. Then store the results in a list collection variable.
public with sharing class EventRegistrationSearchController {
public String searchName { get; set; }
public String searchEvent { get; set; }
public String searchSession { get; set; }
public String searchStatus { get; set; }
public Date regDate { get; set; }
public List<Event_Registration__c> registrations { get; set; }
public EventRegistrationSearchController() {
registrations = new List<Event_Registration__c>();
}
public void search() {
String query = 'SELECT Name__c, Email__c, Phone__c, Registration_Date__c, ' +
'Event_Name__c, Session__c, Status__c ' +
'FROM Event_Registration__c WHERE Id != null';
if (!String.isBlank(searchName)) {
query += ' AND Name__c LIKE \'%' + String.escapeSingleQuotes(searchName) + '%\'';
}
if (!String.isBlank(searchEvent)) {
query += ' AND Event_Name__c = \'' + String.escapeSingleQuotes(searchEvent) + '\'';
}
if (!String.isBlank(searchSession)) {
query += ' AND Session__c = \'' + String.escapeSingleQuotes(searchSession) + '\'';
}
if (!String.isBlank(searchStatus)) {
query += ' AND Status__c = \'' + String.escapeSingleQuotes(searchStatus) + '\'';
}
if (regDate != null) {
query += ' AND Registration_Date__c >= :regDate';
}
registrations = Database.query(query);
}
}Visualforce Page: Display Search Records in Salesforce
Now, create a Visualforce page that displays fields, an input text field, a search button, and result records in a table.
First, we need to define the controller that will provide the data and logic for the Visualforce page. The controller retrieves the registration records and displays them in a table based on the search fields.
We created an Apex class (EventRegistrationSearchController), and its name needs to be passed as the controller in the Visualforce page. This way, the page can use the logic and data from that Apex class.
Next, we defined the inputText tag to pass the Apex variable. That means when the user provides any name in this text, it will be passed to the Apex class, and the query will then retrieve records based on those values.
We also added the picklist values for the fields in the event registration object.
The <apex:pageBlockTable> tag is used to create a table in a Visualforce page. It displays a list of records in rows and columns. In this, we provided the {!registrations} value, which we declared in the Apex class, and stored the searched records.
<apex:page controller="EventRegistrationSearchController" docType="html-5.0">
<apex:form>
<apex:pageBlock title="Event Registration Search">
<apex:pageBlockSection columns="2">
<!-- Search by Name -->
<apex:inputText value="{!searchName}" label="Name" />
<!-- Event Name Picklist -->
<apex:selectList value="{!searchEvent}" size="1" label="Event Name">
<apex:selectOption itemLabel="-- All --" itemValue=""/>
<apex:selectOption itemLabel="Dreamforce" itemValue="Dreamforce"/>
<apex:selectOption itemLabel="TrailblazerDX" itemValue="TrailblazerDX"/>
<apex:selectOption itemLabel="Agentforce + Einstein Copilot sessions" itemValue="Agentforce + Einstein Copilot sessions"/>
<apex:selectOption itemLabel="Salesforce Service Summit" itemValue="Salesforce Service Summit"/>
<apex:selectOption itemLabel="Salesforce World Tour Essentials" itemValue="Salesforce World Tour Essentials"/>
<apex:selectOption itemLabel="Tableau Conference" itemValue="Tableau Conference"/>
<apex:selectOption itemLabel="Salesforce Basecamp" itemValue="Salesforce Basecamp"/>
</apex:selectList>
<!-- Session Picklist -->
<apex:selectList value="{!searchSession}" size="1" label="Session">
<apex:selectOption itemLabel="-- All --" itemValue=""/>
<apex:selectOption itemLabel="Morning Session (9 AM – 12 PM)" itemValue="Morning Session (9 AM – 12 PM)"/>
<apex:selectOption itemLabel="Afternoon Session (1 PM – 4 PM)" itemValue="Afternoon Session (1 PM – 4 PM)"/>
<apex:selectOption itemLabel="Q&A Session (4:30 PM – 5:30 PM)" itemValue="Q&A Session (4:30 PM – 5:30 PM)"/>
</apex:selectList>
<!-- Status Picklist -->
<apex:selectList value="{!searchStatus}" size="1" label="Status">
<apex:selectOption itemLabel="-- All --" itemValue=""/>
<apex:selectOption itemLabel="Registered" itemValue="Registered"/>
<apex:selectOption itemLabel="Attended" itemValue="Attended"/>
<apex:selectOption itemLabel="Cancelled" itemValue="Cancelled"/>
</apex:selectList>
<apex:input type="date" value="{!regDate}" label="Registration Date"/>
</apex:pageBlockSection>
<apex:pageBlockButtons>
<apex:commandButton value="Search" action="{!search}"/>
</apex:pageBlockButtons>
<!-- Results Table -->
<apex:pageBlockTable value="{!registrations}" var="reg">
<apex:column value="{!reg.Name__c}" headerValue="Name"/>
<apex:column value="{!reg.Email__c}" headerValue="Email"/>
<apex:column value="{!reg.Phone__c}" headerValue="Phone"/>
<apex:column value="{!reg.Registration_Date__c}" headerValue="Registration Date"/>
<apex:column value="{!reg.Event_Name__c}" headerValue="Event Name"/>
<apex:column value="{!reg.Session__c}" headerValue="Session"/>
<apex:column value="{!reg.Status__c}" headerValue="Status"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>Proof of Concept:
In the image below, you can see the custom search page we built using Apex and Visualforce. It displays the text fields from the Event Registration object, allowing users to enter search values and quickly find matching records.
Now, as I search records by the Session field and click the Search button, the page instantly shows only those records that match the entered session value.

Now I have the records with the same session time. Next, I want to check the records for registrations that were canceled. For that, I will select Canceled in the Status field and click the Search button.
The page will then show only those records that match both conditions: the selected session and the canceled status.

In this way, we can add multiple searches using Visualforce and Apex in Salesforce.
Custom Search vs Standard Search in Salesforce
| Feature | Standard Search | Custom Search |
|---|---|---|
| Multiple Filters | Limited | Yes |
| Flexibility | Low | High |
| UI Control | No | Yes |
| Performance | Good | Depends on code |
| Custom Logic | No | Yes |
Frequently Asked Questions
1. Can we use LWC instead of Visualforce?
Yes, LWC is modern and recommended. But Visualforce is still used in legacy systems.
2. Is this better than SOSL?
Not always. SOSL is better for searching multiple objects. Custom search is better for filtered queries.
3. Can we add pagination?
Yes, using OFFSET or wrapper classes.
4. Is it secure?
Yes, if you properly prevent SOQL injection.
5. Can we deploy this to production?
Yes, with a proper test class and validation.
Conclusion
Creating a custom search page using Visualforce and Apex is a powerful way to enhance user experience in Salesforce. It allows users to search records more efficiently using multiple filters and dynamic logic.
If implemented correctly with best practices, it can significantly improve productivity and provide a better interface than standard search.
You may like to read:
- Create a Visualforce Page with Apex Controller in Salesforce
- Embed Lightning Web Component In Visualforce Page
- Visualforce Page in Lightning Web Component
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.