Development questions
Write a SOQL query using DISTANCE and GEOLOCATION to find all Warehouse Accounts within 25 miles of a given latitude and longitude.
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
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- What is the difference between before triggers and after triggers?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?