← Back to Blogs LWC

Building a Proximity Account Finder in Salesforce

content image

Modern businesses often need more than just data — they need location intelligence. In this project, the client wanted a System Viewer that could select a Source Account, identify other Accounts within a specific radius, filter them using business criteria, view results in a list or on a map, generate a PDF report, and display the distance in miles for each result. The goal? Help end users quickly identify nearby machines (Accounts) and make faster operational decisions.

🎯 Project Objective

From a single Source Account, users should be able to enter a radius in miles, apply additional filter criteria, retrieve nearby Accounts (Proximity Accounts), and view distance from source, machine details, cost, and last sale information. They can also choose to generate a PDF report or view locations on a map. If results are displayed in a list, the distance in miles must be clearly visible.

🧠 Functional Overview

Step 1: Select Source Account

Any Account in the system can act as a Source. The user initiates the search from that record.

Step 2: Apply Search Filters

The system displays a filter screen where users can configure: Radius (Miles) with an editable default value, Machine Type as a multi-select, Last Sale Days Ago, Cost Range, and other custom fields already present on Account. Users can override default values before running the search.

Step 3: View Search Results

After clicking Search, the system calculates distance between the Source and other Accounts, filters based on radius, applies additional business criteria, and displays results in a structured list. Each row shows Account Name, Distance in miles, Machine Type, Cost, and Last Sale Days Ago. Distance calculation is handled using Latitude and Longitude fields on Account with the Haversine formula, or Salesforce geolocation SOQL.

🗺 Option 1: View Map

Users can switch to a Map Viewer to visualize nearby Accounts. This helps in identifying cluster areas, planning logistics, and understanding geographical distribution. The map view displays a source location marker, destination account markers, and an optional info window with machine details.

📄 Option 2: Generate PDF

After selecting filters and reviewing results, users can click Generate PDF. The generated report includes Source Account details, applied filter criteria, list of Proximity Accounts, distance in miles, and cost and machine information. This is useful for customer discussions, field operations, management reporting, and offline reference.

⚙️ Technical Architecture Overview

This solution was implemented using an Apex Controller, Lightning Web Components for the UI, Geolocation fields on the Account object, Map integration, and PDF generation logic. A core controller handles Source Account retrieval, radius configuration, filter logic, distance calculation, map dataset preparation, and PDF dataset preparation.

Key Data Elements

VariablePurpose
recordIdSource Account ID
SourceAccountFull Source record
radiusDisRadius in miles
Last_Sale_Days_AgoFilter value
MachinesMachine filter
accountListSearchedFiltered proximity results
accountListSearched1Map dataset
SearchAccountListWithMilesAccounts with calculated distance
listIsEmptyUI control flag
headerDataReport header
urlMap redirection
selectedValueFilter selection
sourceAddressSource location string
otherAccountAddressCompared location

📐 How Distance Is Calculated

Option 1: SOQL Geolocation Query

ProximityController.cls
List<Account> nearbyAccounts = [
    SELECT Id, Name,
           DISTANCE(BillingAddress, GEOLOCATION(:sourceLat, :sourceLon), 'mi') dist
    FROM Account
    WHERE DISTANCE(BillingAddress, GEOLOCATION(:sourceLat, :sourceLon), 'mi') < :radiusMiles
    ORDER BY DISTANCE(BillingAddress, GEOLOCATION(:sourceLat, :sourceLon), 'mi')
];

Option 2: Haversine Formula in Apex

HaversineHelper.cls
public static Double calculateDistance(Double lat1, Double lon1, Double lat2, Double lon2) {
    Double R = 3958.8; // Earth radius in miles
    Double dLat = Math.toRadians(lat2 - lat1);
    Double dLon = Math.toRadians(lon2 - lon1);
    Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
             + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
             * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return R * c; // Distance in miles
}

🧩 Common Challenges Solved

ChallengeSolution
Multiple API callsOptimized SOQL filtering
Incorrect distanceGeolocation precision
UI performanceControlled rendering
Large datasetsFilter-first design
User flexibilityOverride defaults

🏁 Final Thoughts

This Proximity Account Finder is more than just a search tool. It combines geolocation intelligence, business filtering, visualization, and reporting — all within Salesforce. By allowing users to filter smartly, view geographically, analyze strategically, and export professionally, it transforms Account data into actionable insight. And that's the real power of building intelligent Salesforce solutions.

Welcome Back

OR
Don't have an account? Sign up