0

I have a SOQl query calculating the distance between two sets of coordinates. In the select statement the alias name for this calculated distance is ‘dist’ I have a pageblocktable in my visual force page and would like to display the alias ‘dist’ as a column value. I tried the below

SOQL query:

projects = [Select Id, Name, DISTANCE( Decimal_LatLon__c,GEOLOCATION( :latt, :lonn)  , 'mi') dist, Client_Account__r.Name, Site_Name__c, Site_Address__c, Site_City__c, CreatedDate, Coordinate_Type__c, Ordering_Company__r.Name from pse__Proj__c  where DISTANCE( Decimal_LatLon__c,GEOLOCATION( :latt, :lonn)  , 'mi') < 1.0 
order by DISTANCE( Decimal_LatLon__c,GEOLOCATION( :latt, :lonn)  , 'mi')]

Used the below syntax to display in visual force page. The page complies

<apex:column value="${!pcon['dist']}" headerValue="Distance" />

But I get the following error. Based on the error, it seems that it is complaining because ‘dist’ is not a field in the Sobject pse__Proj__c. Is there a way to display the value of an alias column in Visual force page?

Invalid field dist for SObject pse__Proj__c Error is in expression '${!pcon['dist']}' in component in page sitesearchpage Error evaluating dynamic reference 'dist'

VF pageblocktable

<apex:pageBlockTable var="pcon" value="{!projs}" >
<apex:column headerValue="Project Number">
<apex:outputLink value="{!sandURL}{!pcon.Id}" target="_blank">{!pcon.Name}</apex:outputLink>
</apex:column>
<apex:column headerValue="Ordering Company">
<apex:outputField value="{!pcon.Ordering_Company__r.Name}"/>
</apex:column>
<apex:column headerValue="Site Name">
<apex:outputField value="{!pcon.Site_Name__c}"/>
</apex:column>
<apex:column headerValue="Site Address">
<apex:outputField value="{!pcon.Site_Address__c}"/>
</apex:column>
<apex:column headerValue="Site City">
<apex:outputField value="{!pcon.Site_City__c}"/>
</apex:column>
<apex:column headerValue="Client Company">
<apex:outputField value="{!pcon.Client_Account__r.Name}"/>
</apex:column>
<apex:column headerValue="Created Date">
<apex:outputField value="{!pcon.CreatedDate}"/>
</apex:column>
<apex:column headerValue="Coordinate Type">
<apex:outputLabel value="{!IF(pcon.Coordinate_Type__c == 2, 'Full Address', IF(pcon.Coordinate_Type__c == 3, 'City State', IF(pcon.Coordinate_Type__c == 5, 'Zip', IF(pcon.Coordinate_Type__c == 1, 'User Specified', ' '))))}" />
</apex:column>
<apex:column value="${!pcon['dist']}" headerValue="Distance" />
<!--<apex:outputField value="${!pcon['dist']}"/> -->
<!--</apex:column> -->
</apex:pageBlockTable>

In the controller, the property 'projs' is defined as below

public List<pse__Proj__c> projs{
        get{
            if (setConProj != null) {
                return (List<pse__Proj__c>)setConProj.getRecords();
            }
            else {
                return null;
            }
        }
        set;
    }
2
  • Have you tried moving it to outputText? That is, <apex:column><apex:outputText value="{!pcon['dist']}" /></apex:column>?
    – Adrian Larson
    Commented Sep 12, 2016 at 20:00
  • When I moved it to outputText, I get a similar error picking on apex:outputtext component Invalid field dist for SObject pse__Proj__c Error is in expression '{!pcon['dist']}' in component <apex:outputText> in page sitesearchpage Error evaluating dynamic reference 'dist'
    – Neema
    Commented Sep 12, 2016 at 20:38

1 Answer 1

1

It does not appear that displaying distance is possible in VF.

If you debug your code in Apex you will notice results are displayed for the mx value.

However, if you simply output {!projs} on your VF page you will only see the ID and Name values. nothing is displayed for the Distance Calculation.

What you can do is create a wrapper for the values and assign the calculation to a property in the wrapper:

public class accountWithDistance{

    public Account a {get;set;}
    public Decimal dist {get;set;}

    public accountWithDistance(Account acc){
       a = acc;
       dist = (Decimal)acc.get('mx');
    }


}

Use the wrapper for the repeat and the dist for the distance calculation. This does work

Curious as to why you have a $ to output with the distance?

2
  • Thank You Eric. I was able to implement using a wrapper class. I also need pagination. So I used apex-lang package for paging through wrapper objects. I am using the '$' sign because someone online had suggested it would work. The code compiled but threw an error. When you say 'mx' value, what are you referring to, the distance? When you say simply output projs to VF, do you mean omitting the distance alias column from the pageblocktable?
    – Neema
    Commented Sep 13, 2016 at 15:23
  • Great. And you can remove the $.
    – Eric
    Commented Sep 13, 2016 at 15:27

You must log in to answer this question.

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