Salesforce Developer Tutorial – How to Calculate the Distance Between Records Using Data Integration Rules and SOQL Distance Queries in Salesforce
Have you ever needed to find all the Contacts within 10 miles of an office? Or maybe locate the nearest Leads to a traveling sales rep?
Typically, geo-location features like this require integrating with third-party tools or external maps APIs. However, Salesforce has a powerful native feature hidden in plain sight that can do this for you: Data Integration Rules.
In this post, we’re breaking down how to generate geo-coordinates and use SOQL to find the distance between records—all without leaving the platform.
Step 1: Activate Data Integration Rules
The first piece of the puzzle is getting the Latitude and Longitude for your records. You don’t need to manually look these up.
- Navigate to Setup.
- Search for and select Data Integration Rules.
Here, you will find a list of pre-built rules for standard objects like Account, Contact, and Lead. These rules can automatically sync geo-coordinates based on standard address fields (e.g., Billing Address or Shipping Address).
For example, you might see a rule labeled something like “Geocodes for Account Billing Address.”
- Activate the rule relevant to your needs.
Once activated, Salesforce will process your records. After a few minutes, your records will automatically be populated with the correct Latitude and Longitude values corresponding to the address fields you selected.
Step 2: Querying with SOQL
Once your records have geo-data, the real magic happens in your code. You can use the DISTANCE function in a SOQL query to filter results based on proximity.
Here is the logic you would use in your Apex code:
- Query the “Center” Record: First, get the specific account or record you want to measure from. You need its specific Latitude and Longitude.
- Query the “Nearby” Records: Use the
DISTANCEclause in a second query to find records within a specific radius.
The Code Example
Here is a simplified example of how the SOQL query looks. This snippet finds all accounts within 10 miles of a target account’s location.
Apex
// Assume targetLat and targetLong are the latitude and longitude coordinates from the record we are measuring //from
// We are using the 'ShippingAddress' field which now has geo-coordinates
List<Account> nearbyAccounts = [
SELECT Id, Name, ShippingAddress
FROM Account
WHERE DISTANCE(ShippingAddress, GEOLOCATION(:targetLat, :targetLong), 'mi') < 10
];
ShippingAddress: The compound field on the record being queried.GEOLOCATION(...): The coordinates of your starting point (the target account).'mi': The unit of measurement (miles). You can also use'km'.
Why This Matters
This native approach is incredibly powerful because:
- No API Costs: You aren’t paying for calls to Google Maps or other external geocoding services.
- Speed: It happens directly on the Salesforce platform.
- Flexibility: You can pipe these results into Lightning Web Components (LWC) to display maps, lists of nearby clients, or route planning tools.
By leveraging Data Integration Rules and standard SOQL, you unlock robust location-based features with zero external dependencies.
Need help with your Salesforce Org?
If you need help with your Salesforce org, schedule an hour of consulting time with me! I’m one of only ~500 Salesforce Certified Technical Architect’s (CTA) worldwide and I’ve spent over 30,000 hours building Salesforce implementations over the last decade!
Schedule and hour of consulting with me here!
Do you want to be the next Salesforce Certified Technical Architect (CTA)?
If you need training to help you on your journey to complete your Salesforce CTA Board then why not sign up for the cheapest Salesforce CTA course out there, with someone who has training over a million Salesforce professionals worldwide! You can check out and enroll in the course below!
Sign up for the CTA course here!
Or if a course isn’t your thing, you can always sign up for an hour long CTA study session with me here:
Schedule 1-on-1 CTA Coaching Here!
Get Coding With The Force Merch!!
We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.
Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!
Check Out More Coding With The Force Stuff!
If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!
Youtube
Patreon
Github
Facebook
Twitter
Instagram
Salesforce Development Books I Recommend
Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps
Good Non-SF Specific Development Books:
Clean Code
Clean Architecture
AI DISCLAIMER: Gemini assisted me in writing this blog post by analyzing and summarizing the contents on the YouTube video I created that is linked at the top of this post.