Create a Custom Search Page in Salesforce With Visualforce and Apex : Bijay Kumar

Create a Custom Search Page in Salesforce With Visualforce and Apex
by: Bijay Kumar
blow post content copied from  SalesForce FAQs
click here to view original post



### Summary of Content: Creating a Custom Search Page in Salesforce The article discusses how to create a custom search page in Salesforce using Visualforce and an Apex Controller. The motivation for this development arose from the limitations of the standard list view search for the Event Registration records, which only allowed searching specific fields and did not support multiple filters simultaneously. To enhance the search experience, the author outlines the steps to build a custom search page where users can enter criteria across multiple fields at once, making it easier to find specific records without scrolling through long lists. **Key Steps to Create a Custom Search Page:** 1. **Define the Apex Class**: This class contains the logic for retrieving data from the `Event_Registration__c` object. It builds a dynamic SOQL query based on user inputs for different search fields such as Name, Event, Session, Status, and Registration Date. 2. **Visualforce Page Creation**: The Visualforce page is designed to display input fields and a search button, along with a results table. It uses the Apex class as its controller to handle data retrieval and display. 3. **Search Logic**: The search function collects user input, constructs a query, and fetches matching records, which are then displayed in a user-friendly format. 4. **User Interaction**: Users can input various criteria and click the search button to see results that match their specifications, thus allowing for a more refined search experience. ### Additional Insights: - This custom solution provides flexibility and a better user experience compared to the limitations of the standard Salesforce list view. - The article emphasizes the importance of using Apex to manage data logic and Visualforce for user interface design. - Such custom features are especially useful for organizations with large datasets, making data retrieval efficient. ### SEO Hashtags: #Salesforce #Visualforce #ApexController #CustomSearch #EventRegistration #DataRetrieval #SalesforceDevelopment #SOQL #UserExperience #CRM Feel free to explore more about creating custom functionalities in Salesforce to enhance your organization's operational efficiency!


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 records, the list view search only allows us to search in 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 also search records using multiple fields at the same time.

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 searching records easier compared to using the standard list view search.

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 records using multiple fields at the same time, 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 it displayed the results based on the value we entered in the search box.

In the second image, we searched for records using Registration Status. However, in the standard list view, you can see that we cannot search for multiple criteria at the same time.

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.

Salesforce Object List View Search

Apex Class: Retrieve Data and Search Logic in Salesforce

Now, let’s create the Apex class in which we will write the logic to search 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, which are used by the VF page 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 search filters the user enters (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 the Visualforce page that shows fields, input text, and a search button with 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, this will pass to the Apex class, and then the query will retrieve records according to the 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, where users can enter their 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.

Visualforce Page and an Apex Controller in Salesforce

Now I have the records with the same session time. Next, I want to check the records where the registration was cancelled. For that, I will select Cancelled 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 cancelled status.

Create a Custom Search Page in Salesforce with Visualforce and Apex

In this way, we can add multiple searches using Visualforce and Apex in Salesforce.

Conclusion

I hope you have got an idea about how to create a custom search page in Salesforce using a Visualforce Page and an Apex Controller. I have explained how a custom search page can make searching records easier compared to using the standard list view search.

You may like to read:

The post Create a Custom Search Page in Salesforce With Visualforce and Apex appeared first on SalesForce FAQs.


August 12, 2025 at 04:31PM
Click here for more details...

=============================
The original post is available in SalesForce FAQs by Bijay Kumar
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce