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:column >
<apex:facet name="header">Volunteer Name</apex:facet>
<apex:outputLink value="/{!VolRecord.Id}" target="_blank">{!VolRecord.Name}</apex:outputLink>
</apex:column>
<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:column >
<apex:facet name="header">Phone 1 </apex:facet>
<apex:outputText value="{!VolRecord.basic_phone1__c}"/>
</apex:column>
<apex:column >
<apex:facet name="header">Phone 2 </apex:facet>
<apex:outputText value="{!VolRecord.basic_phone2__c}"/>
</apex:column>
<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:column >
<apex:facet name="header">Email 2</apex:facet>
<apex:outputLink value="mailto:{!VolRecord.basic_email2__c}">{!VolRecord.basic_email2__c}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Sample Result