🔍 Advanced Search in Salesforce – Use Case & Solution
Salesforce is a powerful CRM platform, but as organizations grow, finding records quickly can become challenging. Standard search and list views are useful, but they often fall short when users need to filter data across multiple fields or objects dynamically.
This is where Advanced Search comes in. It allows users to search efficiently across multiple objects and fields, apply filters, and get actionable results in a single interface.
📌 What is Advanced Search?
Advanced Search in Salesforce refers to a custom search solution that goes beyond standard Salesforce search functionality. It is typically built using:
- Apex – to dynamically query Salesforce objects.
- SOSL/SOQL – to perform text-based or field-specific searches.
- Lightning Web Components (LWC) – to create an intuitive, user-friendly search interface.
With Advanced Search, you can:
- Search across multiple objects simultaneously.
- Apply complex conditions with AND/OR logic.
- Filter results dynamically based on user input.
- Display results in an interactive table with actionable buttons.
📌 Real-World Use Case
Scenario:
A Finance Department uses Salesforce to manage Invoices, Accounts, and Payments. Finance managers often need to:
- Find invoices based on multiple fields such as Invoice Number, Account Name, or Status.
- Filter Payments by Amount, Payment Date, or Payment Method.
- Search Accounts by Industry, Region, or Annual Revenue.
- Perform all these searches from a single interface to save time.
📌 Solution – Building an Advanced Search
We can implement Advanced Search with the following components:
- Apex Class – Dynamically builds queries based on user filters.
- Lightning Web Component (LWC) – Provides an interactive search interface.
- SOSL/SOQL – Fetches the results efficiently.
🔹 Step 1: Apex Controller
public with sharing class FinanceSearchController {
@AuraEnabled(cacheable=true)
public static List<sObject> searchRecords(String objectName, Map<String, String> filters) {
String query = ‘SELECT Id, Name FROM ‘ + objectName + ‘ WHERE ‘;
List<String> conditions = new List<String>();
for(String field : filters.keySet()) {
String value = filters.get(field);
if(value != null && value != ”) {
conditions.add(field + ‘ LIKE \’%’ + String.escapeSingleQuotes(value) + ‘%\”);
}
}
if(conditions.isEmpty()) {
query = ‘SELECT Id, Name FROM ‘ + objectName + ‘ LIMIT 50’;
} else {
query += String.join(conditions, ‘ AND ‘) + ‘ LIMIT 50’;
}
return Database.query(query);
}
@AuraEnabled(cacheable=true)
public static List<String> getSearchableObjects() {
return new List<String>{‘Invoice__c’, ‘Account’, ‘Payment__c’};
}
}
✅ This Apex class dynamically builds SOQL queries based on user-selected filters.
🔹 Step 2: LWC HTML
<template>
<lightning-card title=”Finance Advanced Search” icon-name=”standard:filter”>
<div class=”slds-p-around_medium”>
<!– Object Selection –>
<lightning-combobox
label=”Select Object”
value={selectedObject}
options={objectOptions}
onchange={handleObjectChange}>
</lightning-combobox>
<!– Dynamic Filters –>
<template for:each={filters} for:item=”filter”>
<div key={filter.field} class=”slds-m-around_x-small”>
<lightning-input
label={filter.label}
data-field={filter.field}
value={filter.value}
onchange={handleFilterChange}>
</lightning-input>
</div>
</template>
<!– Buttons –>
<div class=”slds-m-top_small”>
<lightning-button label=”Search” variant=”brand” onclick={handleSearch}></lightning-button>
<lightning-button label=”Clear” onclick={handleClear}></lightning-button>
</div>
</div>
<!– Results Table –>
<template if:true={searchResults}>
<lightning-datatable
key-field=”Id”
data={searchResults}
columns={columns}>
</lightning-datatable>
</template>
</lightning-card>
</template>
✅ The UI provides object selection, dynamic filters, and results display.
🔹 Step 3: LWC JavaScript
import { LightningElement, track, wire } from ‘lwc’;
import searchRecords from ‘@salesforce/apex/FinanceSearchController.searchRecords’;
import getSearchableObjects from ‘@salesforce/apex/FinanceSearchController.getSearchableObjects’;
export default class FinanceAdvancedSearch extends LightningElement {
@track selectedObject = ”;
@track objectOptions = [];
@track filters = [];
@track searchResults;
@track columns = [
{ label: ‘Record Name’, fieldName: ‘Name’, type: ‘text’ },
{ type: ‘button-icon’, typeAttributes: { iconName: ‘utility:delete’, title: ‘Delete’, variant: ‘bare’ } }
];
connectedCallback() {
getSearchableObjects().then(result => {
this.objectOptions = result.map(obj => ({ label: obj, value: obj }));
});
}
handleObjectChange(event) {
this.selectedObject = event.detail.value;
this.filters = [];
if(this.selectedObject === ‘Invoice__c’) {
this.filters = [
{ label: ‘Invoice Number’, field: ‘Invoice_Number__c’, value: ” },
{ label: ‘Status’, field: ‘Status__c’, value: ” },
{ label: ‘Account Name’, field: ‘Account__r.Name’, value: ” }
];
} else if(this.selectedObject === ‘Payment__c’) {
this.filters = [
{ label: ‘Payment Date’, field: ‘Payment_Date__c’, value: ” },
{ label: ‘Amount’, field: ‘Amount__c’, value: ” },
{ label: ‘Payment Method’, field: ‘Payment_Method__c’, value: ” }
];
} else if(this.selectedObject === ‘Account’) {
this.filters = [
{ label: ‘Account Name’, field: ‘Name’, value: ” },
{ label: ‘Industry’, field: ‘Industry’, value: ” },
{ label: ‘Region’, field: ‘BillingCity’, value: ” }
];
}
}
handleFilterChange(event) {
const field = event.target.dataset.field;
const value = event.target.value;
this.filters = this.filters.map(f => f.field === field ? { …f, value } : f);
}
handleSearch() {
let filterMap = {};
this.filters.forEach(f => { if(f.value) filterMap[f.field] = f.value; });
searchRecords({ objectName: this.selectedObject, filters: filterMap })
.then(result => { this.searchResults = result; })
.catch(error => { console.error(error); });
}
handleClear() {
this.filters = this.filters.map(f => ({ …f, value: ” }));
this.searchResults = [];
}
}
✅ Handles dynamic filter updates, executes search, and displays results.
📌 Benefits of This Finance Advanced Search
- Saves Time: Users can find invoices, payments, and accounts in one interface.
- Dynamic Filters: Easily add/remove fields without changing Apex logic.
- Customizable: Works with standard and custom objects.
- User-Friendly: Table view with actions (delete, view, edit) improves productivity.
📌 Business Scenarios
- Finance team verifies overdue invoices by filtering Status = Pending and Due Date < Today.
- Accounts team searches for high-value accounts by Industry = Banking and Annual Revenue > 1M.
- Payment team quickly identifies failed transactions for reconciliation.
🚀 Conclusion
Advanced Search in Salesforce empowers users to retrieve exactly what they need, when they need it. By combining Apex, SOQL/SOSL, and Lightning Web Components, you can build dynamic, reusable, and interactive search interfaces that fit your business processes.