I'm working with a local non-profit group and trying to help them display a list of 50 closest volunteers within 100 miles of a given site.

The custom VF page is displaying all of the values properly with the exception of the distance field. Due to apparent limitations in selecting back Distance as part of the SOQL, I'm setting the distance result as a variable using a for loop. However, I think this is being done (or displayed) incorrectly, as I always get the LAST value of the variable back on all records

I'll freely admit that I'm fairly new to SF and have googled and tried this every way I can think of, including apex:repeat, but get the same result back.

Here's the code I'm using:

Apex Class

public with sharing class ShowNearbyVolunteers {

Decimal dlat = decimal.valueOf(apexpages.currentPage().getParameters().get('lat'));
Decimal dlon = decimal.valueOf(apexpages.currentPage().getParameters().get('lon'));

public List<Volunteer__c> VolRecords {get; set;} 
transient public Decimal dist1 {get;set;}
public ShowNearbyVolunteers(){ 
VolRecords = 
[select DISTANCE(GeoLocation__c,GEOLOCATION(:dlat, :dlon),'mi') dist, Id, Name, admin_position_type__c, admin_title__c, basic_phone1__c, basic_phone2__c, basic_email__c, basic_email2__c 
from Volunteer__c 
where admin_status__c = 'Active' and DISTANCE( Geolocation__c, GEOLOCATION( :dlat, :dlon ), 'mi' ) < 100
order by DISTANCE( Geolocation__c, GEOLOCATION( :dlat, :dlon ), 'mi' ) asc
limit 50]; 

 for ( Volunteer__c vol : VolRecords ) {
     dist1 = ((Decimal)vol.get('dist')).setScale(3);

VF Page

<apex:page controller="ShowNearbyVolunteers">

    <apex:pageBlock title="Nearest 50 Volunteers within 100mi of Site: {!$CurrentPage.parameters.site}"> 
        <apex:pageBlockTable value="{!VolRecords}" var="VolRecord"> 
            <apex:column > 
                <apex:facet name="header">Distance (mi)</apex:facet> 
                <apex:outputText value="{!dist1}"/> 
            <apex:column > 
                <apex:facet name="header">Volunteer Name</apex:facet> 
                <apex:outputLink value="/{!VolRecord.Id}" target="_blank">{!VolRecord.Name}</apex:outputLink>
            <apex:column > 
                <apex:facet name="header">Position Type (Title)</apex:facet> 
                <apex:outputText value="{!VolRecord.admin_position_type__c} ({!VolRecord.admin_title__c})"/> 
            <apex:column > 
                <apex:facet name="header">Phone 1 </apex:facet> 
                <apex:outputText value="{!VolRecord.basic_phone1__c}"/> 
            <apex:column > 
                <apex:facet name="header">Phone 2 </apex:facet> 
                <apex:outputText value="{!VolRecord.basic_phone2__c}"/> 
            <apex:column > 
                <apex:facet name="header">Personal Email</apex:facet> 
                <apex:outputLink value="mailto:{!VolRecord.basic_email__c}">{!VolRecord.basic_email__c}</apex:outputLink>
            <apex:column > 
                <apex:facet name="header">Email 2</apex:facet> 
                <apex:outputLink value="mailto:{!VolRecord.basic_email2__c}">{!VolRecord.basic_email2__c}</apex:outputLink>



Sample Result

enter image description here


If I'am getting this correctly it appears that you are overwriting the dist1 value. What you need to so is to get all the items in a list or preferably a map and loop through them.

Add the following outside of the method:

public Map<Id,Decimal> volunteerDistanceMap {get;set;}

You can try creating a map like the following:

volunteerDistanceMap = new Map<Id,Decimal>();

for ( Volunteer__c vol : VolRecords ) {

Then on your VF page loop over it like this:

<apex:column > 
  <apex:facet name="header">Distance (mi)</apex:facet> 
  <apex:outputText value="{!volunteerDistanceMap[VolRecord.Id]}"/> 
| improve this answer | |
  • After adding the top code to Apex, am getting an error when trying to add the Map on the VF Page: Error: Unknown property 'ShowNearbyVolunteers.volunteerDistanceMap – Talphius Feb 16 '18 at 15:45
  • @Talphius I've edited the answer. Add the get set methods and it should be visible. – Kostadin Stoev Feb 16 '18 at 15:55
  • AMAZING -- you don't know how much time I've spent banging my head on this, for such a seemingly easy answer. Thank you so much! – Talphius Feb 16 '18 at 16:04

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.