Skip to main content

This blog post delves into the development journey of a Lightning Web Component (LWC) intricately linked with the Salesforce Apex controller. This component is engineered to efficiently retrieve data and present it within a Lightning Datatable, augmented by a sophisticated search bar functionality, empowering users to dynamically filter and pinpoint specific records.


Within the Apex Controller (SearchControllerInDatatable), the fetch data method is prominently annotated with @AuraEnabled. This method meticulously fetches a curated collection of Account records, encompassing vital attributes such as ID, Name, and a bespoke Email__c field. Noteworthy is the incorporation of the public sharing keyword, exemplifying the Apex class’s steadfast adherence to Salesforce’s stringent sharing regulations.


public with sharing class SearchControllerInDatatable{
    public static List<Account> getData() {
        return [SELECT Id, Name, Email__c FROM Account];
  • Imports LightningElement and wire from ‘lwc’.
  • Utilizes the @wire decorator to import the fetchData Apex method.
  • Defines a set of columns for the Lightning Datatable, specifying labels, field names, and types.
  • Implements a search bar using the <lightning-input> component and binds it to the searchTerm property.
  • Utilizes a debounce mechanism to optimize performance by delaying the execution of the filtering logic.
  • Implements the filterData method to dynamically filter records based on the search term.

Metadata Configuration (XML File):

  • The Lightning component bundle is configured for exposure and targeted for various Salesforce page types, including Record Page, App Page, Home Page, and Tab.


        <lightning-input type="search" label="Search" value={searchTerm} onchange={handleSearchChange}>
        <lightning-datatable key-field="id" data={filteredData} columns={columns} hide-checkbox-column="true">


import { LightningElement, wire } from 'lwc';
import getData from '@salesforce/apex/SearchControllerInDatatable.getData';

const columns = [
    { label: 'Name', fieldName: 'Name', type: 'text' },
    { label: 'Email', fieldName: 'Email__c', type: 'Email' }

export default class SearchBarInDatatable extends LightningElement {
    searchTerm = '';        
    data = [];              
    filteredData = [];      

    wiredData({ error, data }) {
        if (data) {
   = data;
        } else if (error) {
            console.error('Error loading data: ', error);

    get columns() {
        return columns;

    handleSearchChange(event) {
        this.searchTerm =;

    debounceFilter() {
        this.delayTimeout = setTimeout(() => {
        }, 300);

    filterData() {
        this.filteredData = =>
            Object.values(record).some(value =>

Outcome: Data Table with Search Functionality:

Conclusion: Developing a Lightning Web Component with search capabilities elevates user interaction by facilitating seamless navigation and information retrieval within the Salesforce platform. Leveraging the synergy between Apex and Lightning Web Components empowers developers to craft sophisticated, responsive, and streamlined interfaces that align precisely with organizational requirements. Continuously refine and expand upon the provided code to accommodate evolving needs and optimize user engagement within your Salesforce ecosystem.

Subscribe For More Updates


Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!