Location-Based SOQL Queries in Salesforce

In Salesforce, location-based SOQL queries allow you to retrieve records based on their geographic location. This feature is particularly useful for applications that require proximity-based searches or location-aware functionality. Location-based SOQL queries leverage the Salesforce Object Query Language (SOQL) along with the geolocation fields available in custom objects or standard objects like Account, Contact, and Lead.

Location-Based SOQL Query Considerations

Location-based queries are supported in SOQL in Apex and in the SOAP and REST APIs. Keep in mind these considerations.

DISTANCE and GEOLOCATION are supported in WHERE and ORDER BY clauses in SOQL, but not in GROUP BY. DISTANCE is supported in SELECT clauses.

DISTANCE supports only the logical operators > and <, returning values within (<) or beyond (>) a specified radius.

When using the GEOLOCATION function in SOQL queries, the geolocation field must precede the latitude and longitude coordinates. For example, DISTANCE(warehouse_location__c, GEOLOCATION(37.775,-122.418), 'km') works but DISTANCE(GEOLOCATION(37.775,-122.418), warehouse_location__c, 'km') doesn’t work.

Apex bind variables aren’t supported for the units parameter in the DISTANCE function. This query doesn’t work.

String units = 'mi';
List<Account> accountList = 
    [SELECT ID, Name, BillingLatitude, BillingLongitude 
     FROM Account 
     WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(10,10), :units) < 10];

Geolocation Fields:

Salesforce provides special geolocation fields (Geolocation) that store latitude and longitude coordinates for a specific location. These fields can be added to custom objects or certain standard objects to store location data.

Geolocation fields can be populated manually or automatically using integration with mapping services or external data sources.

SOQL Queries:

Location-based SOQL queries enable you to search for records based on their proximity to a specified location.

You can use the DISTANCE and GEOLOCATION() functions in SOQL to perform location-based queries.

Here's an example of a simple SOQL query to retrieve Accounts within a certain distance from a specified location:

SELECT Id, Name, BillingLatitude, BillingLongitude
FROM Account

WHERE DISTANCE(BillingAddress, GEOLOCATION(latitude, longitude), 'mi') < 10

In this query:

BillingAddress is a compound field that stores the complete address information, including latitude and longitude coordinates.
GEOLOCATION(latitude, longitude) specifies the reference location from which to calculate the distance.'mi' indicates the unit of distance (in this case, miles).

Filtering by Distance:

You can specify the maximum distance from the reference location using the DISTANCE function. Records within the specified distance are returned by the query.
Adjust the distance value as per your requirements to retrieve records within the desired radius.

Optimizing Performance:

Location-based queries may have performance implications, especially when dealing with large datasets.
Consider indexing the geolocation fields to improve query performance, particularly if you frequently perform location-based searches.
Use selective queries and leverage other query optimization techniques to ensure efficient execution.

Supported Objects:

Location-based SOQL queries can be performed on objects that have geolocation fields, such as Account, Contact, and custom objects with geolocation fields.
Ensure that the object's geolocation fields are properly populated with latitude and longitude coordinates for accurate results.

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach