I am opening this question after reading this: Access DISTANCE SOQL Result in Visualforce

I have this query [SELECT id, DISTANCE(some_field__c, GEOLOCATION(0,0), 'mi') distance FROM Parent__c LIMIT 1] and that's working fine.

However when I try to execute this query starting from the child: [SELECT id, DISTANCE(Parent__r.some_field__c, GEOLOCATION(0,0), 'mi') distance FROM Child__c LIMIT 1] I get an error saying No such column 'distance' on entity Child__c. Is this a salesforce limitation or am I doing something wrong?

  • It's a bit of a hack, but try [SELECT id, DISTANCE(GEOLOCATION(Parent__r.some_field__latitude__s, Parent__r.some_field__longitude__s ), GEOLOCATION(0,0), 'mi') distance FROM Child__c LIMIT 1]. This will split out the individual latitude and longitude values from the referenced parent field. Jul 11, 2017 at 19:41
  • @DanielBallinger I can't execute this query. I get a expecting a right parentheses, found ',' for the Distance part of the query. Everything seems aright with the syntax though so i don't know what's wrong.
    – Arthlete
    Jul 11, 2017 at 19:56
  • What about something simpler, like SELECT id, Parent__r.some_field__latitude__s, Parent__r.some_field__longitude__s FROM Child__c LIMIT 1] Jul 11, 2017 at 19:58
  • @DanielBallinger That works but I can't get the distance with this query.
    – Arthlete
    Jul 11, 2017 at 20:01
  • Incredibly it works in the WHERE and the ORDER BY, but it does not appear in the select Mar 23, 2023 at 20:09

1 Answer 1


Known Issue - Distance cannot be selected for foreign key fields (via @fehays)

Distance cannot be selected for foreign key fields, for example use the below query in Apex: [SELECT id, DISTANCE(Parent__r.some_field__c, GEOLOCATION(0,0), 'mi') distance FROM Child__c ], we get an error saying No such column 'distance' on entity Parent object.

I added a Geolocation field to Account. The following SOQL query then worked for me.

    Id, Account.Location__c, DISTANCE(Account.Location__c, GEOLOCATION(0,0), 'mi') distance 
from Contact 
where ID = '003700000000001'

So it should be possible to do distance calculations based on a parents geolocation field.

It isn't clear why mine would work and yours would fail. Try removing the distance alias from the SOQL query. Also, check you API versions.

  • 1
    It works as a query but try to execute it in anonymous apex. I assign this query to an object, let's call it myObject. Then I try Decimal distance = (Decimal)myObject.distance; to get that distance. My query works inside the Query Editor but not inside the anonymous apex.
    – Arthlete
    Jul 11, 2017 at 21:30
  • @Arthlete There appears to be a bug in Apex. Do you have access to support? Jul 11, 2017 at 21:36
  • No, I don't think so
    – Arthlete
    Jul 11, 2017 at 23:55
  • 1
    I contacted support and they have identified it as a bug. Here's a link to the known issue: success.salesforce.com/issues_view?id=a1p3A000000mCfK
    – fehays
    Oct 20, 2017 at 19:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .