I'm trying to do a simple SOQL query to find all opportunities that have a null geolocation field, however, the query keeps blowing with:

Save error: field 'Coordinates__c' can not be filtered in query call

Is it not possible to filter by geolocation fields? I haven't been able to find anything online that says so. I thought it might be because the field wasn't part of the select clause, so I added it in, but it didn't correct the error.

Can someone point me at what I'm missing?

  • I find it funny that the question was put on hold, and the reason for it is because I essentially didn't give example code, which anyone should easily infer from the question's contents, which do describe a specific problem. Nevertheless, since it's such an issue, here's example code: SELECT Id FROM Opportunity WHERE (Coordinates__c != NULL). Funny and sad... – Gup3rSuR4c Aug 16 '17 at 19:35
  • 1
    This community has thrived for a long time on community moderation. 5 of our most experienced users agreed that you didn't provide enough code or context. Sarcasm and condescension are not vehicles to get people to help you. – Sebastian Kessel Aug 16 '17 at 21:05

Take a look at this article on how to use GEOLOCATION fields in SOQL

Essentially you will need to use DISTANCE function that calculates distance between two coordinates and then use GEOLOCATION() function in where clause .

An example query would look like below

SELECT Name, Coordinates__c FROM Warehouse__c WHERE DISTANCE(Coordinates__c, GEOLOCATION(37.775,-122.418), 'mi') < 20 

Also note some limitations

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

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

3.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.

4.Apex bind variables aren’t supported for the units parameter in DISTANCE or GEOLOCATION functions.

|improve this answer|||||
  • So, just the standard Salesforce pointless obstructiveness and Apex uselessness... In the end I decided to simply add a boolean marker field to check against since an arguably simple null check against a geolocation field is not allowed. I'll mark you as the answer only because I can't add my own answer now that the question was pointlessly put on hold. – Gup3rSuR4c Aug 16 '17 at 19:39
  • Turns out I can perform a null check against the underlying fields, like: WHERE (Coordinates__latitude__s != null) AND (Coordinates__longitude__s != null). Since I can do that I removed the marker field I had placed before. – Gup3rSuR4c Aug 17 '17 at 0:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.