4

I am trying to access the result of a DISTANCE SOQL Query in visualforce but I cannot figure out how to do this. The correct values do appear in debugging. Below is my relevant code:

Controller:

engineers = Database.query(
                             'SELECT First_Name__c, Skills__c,  Last_Name__c, Division__c, ' +
                             'Location__Latitude__s, Location__Longitude__s, ' +
                             'DISTANCE(Location__c, GEOLOCATION(' + lat + ',' + lng + '), \'mi\') Miles ' +
                             'FROM Remote_Engineer__c WHERE Name != null');

for (Remote_Engineer__c eng : engineers) {
        System.debug(eng.get('Miles'));
    }
}

Visualforce:

<apex:pageBlock title="Engineers" id="results">
    <apex:pageBlockTable value="{!engineers}" var="e">
        <apex:column headerValue="First Name" title="Engineer First Name">
            <apex:outputLink value="/{!e.Id}">{!e.First_Name__c}</apex:outputLink>
        </apex:column>
        <apex:column headerValue="Last Name" title="Engineer Last Name">
            <apex:outputField value="{!e.Last_Name__c}" />
        </apex:column>
        <apex:column  headerValue="Division" title="Division">
            <apex:outputField value="{!e.Division__c}" />
        </apex:column>
        <apex:column  headerValue="State" title="State">
            <apex:outputField value="{!e.State__c}" />
        </apex:column>
        <apex:column headerValue="Skills" title="Skills">
            <apex:outputField value="{!e.Skills__c}" />
        </apex:column>
            <apex:column headerValue="Latitude" title="Latitude">
            <apex:outputField value="{!e.Location__Latitude__s }" />
        </apex:column>
        <apex:column headerValue="Longitude" title="Longitude">
            <apex:outputField value="{!e.Location__Longitude__s }" />
        </apex:column>
        <apex:column headerValue="Miles" title="Miles">
            <apex:outputtext value="{!e.Miles}" />
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>

Things I have tried:

  • Replaced apex:outputField to apex:outputtext to fix this error:

    Could not resolve field 'Miles' from value binding
    '{!e.Miles}' in page VenueRemoteLookup

  • Replaced {!e.Miles} with {!e['Miles']} which will compile correctly (I am using sublime and mavensmate), but when viewing the visualforce page I get the following error:

    Could not resolve field 'Miles' from value binding '{!e['Miles']}' in page venueremotelookup

  • Shouldn't your query return a List<AggregateResult> rather than any other SObject? – Charles T Nov 3 '16 at 16:16
  • @CharlesT Have you tried it? You will get a compile error. – Adrian Larson Nov 3 '16 at 16:57
1

I haven't yet confirmed this is a feature gap, but you can certainly use a wrapper class. Here's a simple demo:

Controller

public with sharing class DemoController
{
    public List<AccountWrapper> wrappers { get; private set; }

    public DemoController()
    {
        wrappers = new List<AccountWrapper>();
        for (Account record : [
            SELECT Name, DISTANCE(BillingAddress, GEOLOCATION(0,0), 'mi') distance
            FROM Account WHERE BillingLatitude != null
        ]) wrappers.add(new AccountWrapper(record));
    }

    public class AccountWrapper
    {
        public Account record { get; private set; }
        public Decimal distance { get; private set; }
        public AccountWrapper(Account record)
        {
            this.record = record;
            this.distance = (Decimal)record.get('distance');
        }
    }
}

Page

<apex:page controller="DemoController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!wrappers}" var="wrapper">
            <apex:column value="{!wrapper.record.Name}" headerValue="Name" />
            <apex:column headerValue="Distance">
                <apex:outputText value="{!wrapper.distance}" />
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
  • 1
    You beat me. I am in the middle of writing this answer :). – Tushar Sharma Nov 3 '16 at 16:24
  • 1
    This issue seems similar (but not identical) to this Known Issue: SOQL result which uses Distance function is in VF view state causes ISE. – Adrian Larson Nov 3 '16 at 16:24
  • @AdrianLarson I tried your query to reference a geolocation field on the parent and I get "No such column 'distance' on entity". I have no trouble referencing it if I directly query the parent. Is it not possible to do this query starting at the child level and going up to the parent? – Arthlete Jul 7 '17 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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