2

I saw this question, but the answer doesn't help me.

I have a case where I want to measure the distance between a given lat/long and "My Object's" Site's geolocation coordinate. The code is like:

String myId = ...;
String lat = ...;
String lon = ...;

List<MyObject__c> myObjects = (List<MyObject__c>) Database.query(
    'SELECT Id, Name, DISTANCE(Site__r.Coordinates__c, ' +
        'GEOLOCATION(:lat, :lon), \'km\') dist FROM MyObject__c ' +
        'WHERE Id = :myId');

This query works, but even when the related Site object has valid coordinates, and the lat/lon values are valid, getting the value from the MyObject__c instance:

System.debug(myObjects[0].getPopulatedFieldsAsMap().get('dist'));

Log:

11:21:48.41 (124442138)|USER_DEBUG|[14]|DEBUG|null

It is always null.

If the "Coordinates__c" are directly on MyObject__c the above query works. It is something to do with the fact that the coordinates originate from a related object.

Anyone know how to get this query to work with this object structure? (I am working around this by getting the coordinates back and performing an explicit distance calculation in Apex but would rather not.)

2

0

Browse other questions tagged .