Write a SOQL query using DISTANCE and GEOLOCATION to find all Warehouse Accounts within 25 miles of a given latitude and longitude.
Answer
Use DISTANCE() with GEOLOCATION() against a geolocation field (e.g. a custom Location__c on Account) in the WHERE clause and optionally ORDER BY.
soql
SELECT Id, Name, DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi') dist
FROM Account
WHERE DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi') < 25
AND Type = 'Warehouse'
ORDER BY DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi')
Units are 'mi' or 'km'. The location must be a compound geolocation field; DISTANCE can't be used in GROUP BY.
soqlscenariogeolocationdistance