function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Meenakshi T RMeenakshi T R 

Visual force/Apex

I have a problem I am not able to display distance that is aliased int he soql query in VF page. Any help?

public with sharing class GetSupplierListController 
{
     Public List <SCMC__Requisition__c> cs {get; set;}
     Public List<SCMC__Supplier_Site__c> suplist {get; set;}
      String skillset='';
      String producttype='';
      String productbrand='';
      String complexityrating='';
     public GetSupplierListController(ApexPages.StandardController sc) 
    {
        Id idCS = sc.getId();
        cs= new List<SCMC__Requisition__c>();
        suplist = new List<SCMC__Supplier_Site__c>();
        if(idCS != NULL)
        {
            cs = [SELECT Id,Name,Location__c,Location__Latitude__s,Location__Longitude__s,CTC_Skill_Set__c,CTC_Product_Brand__c,CTC_Product_Type__c,CTC_Highlift_Requirement__c,CTC_Complexity_Rating_Stars__c FROM SCMC__Requisition__c WHERE Id =: idCS];
            skillset = cs[0].CTC_Skill_Set__c;
            producttype=cs[0].CTC_Product_Type__c;
            productbrand=cs[0].CTC_Product_Brand__c;
             //highlift==cs[0].CTC_Highlift_Requirement__c;
             complexityrating=cs[0].CTC_Complexity_Rating_Stars__c;
            Location loc = cs[0].Location__c;
            Double lat = loc.latitude;
            Double lon = loc.longitude;          
            if(skillset.contains('Breakdowns'))
            {
                  // fetch supplier with matching values
            suplist = [select id,Name,Location__c,Location__Latitude__s, Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist,Location__Longitude__s, CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_Breakdowns_Rating__c,CTC_Maintenance_Rating__c,CTC_New_Constuction_Rating__c,CTC_Upgrades_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_Breakdowns_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')];                
            }
            else if(skillset.contains('Maintenance'))
            {
                  // fetch supplier with matching values
            suplist = [select id,Name,Location__c,Location__Latitude__s,Location__Longitude__s,Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist, CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_Maintenance_Rating__c,CTC_Breakdowns_Rating__c,CTC_New_Constuction_Rating__c,CTC_Upgrades_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_Maintenance_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')];                
            }
            
            else if(skillset.contains('New Construction'))
            {
                  // fetch supplier with matching values
            suplist = [select id,Name,Location__c,Location__Latitude__s,Location__Longitude__s, Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist,CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_New_Constuction_Rating__c,CTC_Maintenance_Rating__c,CTC_Breakdowns_Rating__c,CTC_Upgrades_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_New_Constuction_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')]; 
                
            }
            else if(skillset.contains('Upgrades'))
            {
                  // fetch supplier with matching values
            suplist = [select id,Name,Location__c,Location__Latitude__s,Location__Longitude__s,Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist, CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_Upgrades_Rating__c,CTC_New_Constuction_Rating__c,CTC_Maintenance_Rating__c,CTC_Breakdowns_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_Upgrades_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')]; 
                
            }

        }
    }
}
ShivankurShivankur (Salesforce Developers) 
Hi Meenakshi,

Please try to add debug lines in the Apex to see if the SOQL returns you the Distance properly.

As a next step, you can post the VF page code as well in this question, if the SOQL returns the Distance correctly, and could be an issue with VF page while displaying the same.

Reference:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_alias.htm

Also,have you ensured that the Location-Based SOQL Query Considerations mentioned in https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_geolocate.htm  are met 

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Meenakshi T RMeenakshi T R
HI @Sivankur. Thank you for the reply. My VF page is pretty straightforward. I am trying to retrieve the aliased dist in my page but I am not able to save it. I'm getting 
Error: Invalid field dist for SObject SCMC__Supplier_Site__c

<apex:page standardController="SCMC__Requisition__c" extensions="GetSupplierListController" lightningStylesheets="true" >
  
   <apex:pageBlock >
     <apex:pageblocktable value="{!suplist}" var="d" columns="9" style="width:900px">

            <apex:column headerValue="Name">
            <apex:outputLink value="/{!d.Id}"><u>{!d.Name}</u></apex:outputLink>
           </apex:column>           
            <apex:column value="{!d.CTC_High_Lift_Available__c}"/>
             <apex:column value="{!d.CTC_Skill_Set__c}"/>
                 <apex:column value="{!d.dist}"/>
             <apex:column value="{!d.CTC_Product_Type__c}"/>
             <apex:column value="{!d.CTC_Product_Brands__c}"/>
              <apex:column value="{!d.CTC_Breakdowns_Rating__c}"/>
               <apex:column value="{!d.CTC_Maintenance_Rating__c}"/>
                <apex:column value="{!d.CTC_New_Constuction_Rating__c}"/>
                 <apex:column value="{!d.CTC_Upgrades_Rating__c}"/>
              <!--   <apex:column headerValue="Distance">
                 <apex:outputText value="{!suplist['dist']}"></apex:outputText>
                    </apex:column>  !-->
    </apex:pageblocktable>  </apex:pageBlock>
  
</apex:page>

 
ShivankurShivankur (Salesforce Developers) 
Hi Meenakshi,

Can you try to use something like below in your VF page:
<apex:column value="${!suplist['dist']}" headerValue="Distance" />
And it seems similar problem have been discussed on this thread (https://salesforce.stackexchange.com/questions/140369/display-value-of-alias-column-from-a-query-in-a-vf-pageblocktable) too.

What you can do is create a wrapper for the values and assign the calculation to a property in the wrapper as described in above thread.

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Meenakshi T RMeenakshi T R
Hi Shivankur,

I tried using <apex:column value="${!suplist['dist']}" headerValue="Distance" /> it got saved but in the page it shows the below error.


Error is in expression '${!d['dist']}' in component <apex:column> in page ctc_getsupplierlistvf
Error evaluating dynamic reference 'dist'


I saw the thread as well not sure how to create wrapper class I am bit new to salesforce can you help me? Thank you
Meenakshi T RMeenakshi T R
Hi Shivankur,

I managed to write the wrapper class

public with sharing class GetSupplierListController 
{
     private final ApexPages.StandardController theController;
     Public List <SCMC__Requisition__c> cs {get; set;}
     Public List<SCMC__Supplier_Site__c> suplist {get; set;}
     Public List<WrapList> countList {get; set;}
      String skillset='';
      String producttype='';
      String productbrand='';
      String complexityrating='';
     public GetSupplierListController(ApexPages.StandardController sc) 
    {
        theController = sc;
    }
       public List<WrapList> getCounts()
       {
         cs= new List<SCMC__Requisition__c>();
        //suplist = new List<SCMC__Supplier_Site__c>();
        countList = new List<WrapList>();
            cs = [SELECT Id,Name,Location__c,Location__Latitude__s,Location__Longitude__s,CTC_Skill_Set__c,CTC_Product_Brand__c,CTC_Product_Type__c,CTC_Highlift_Requirement__c,CTC_Complexity_Rating_Stars__c FROM SCMC__Requisition__c WHERE Id =:theController.getId()];
            skillset = cs[0].CTC_Skill_Set__c;
            producttype=cs[0].CTC_Product_Type__c;
            productbrand=cs[0].CTC_Product_Brand__c;
             //highlift==cs[0].CTC_Highlift_Requirement__c;
             complexityrating=cs[0].CTC_Complexity_Rating_Stars__c;
            Location loc = cs[0].Location__c;
            Double lat = loc.latitude;
            Double lon = loc.longitude;          
            if(skillset.contains('Breakdowns'))
            {
                  // fetch supplier with matching values
            for(SCMC__Supplier_Site__c suplist: [select id,Name,Location__c,Location__Latitude__s, Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist,Location__Longitude__s, CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_Breakdowns_Rating__c,CTC_Maintenance_Rating__c,CTC_New_Constuction_Rating__c,CTC_Upgrades_Rating__c from SCMC__Supplier_Site__c 
                                                 where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_Breakdowns_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')])
            {
                            countList.add(new WrapList(suplist));                           
            }                
            }
            else if(skillset.contains('Maintenance'))
            {
                  // fetch supplier with matching values
            for(SCMC__Supplier_Site__c suplist : [select id,Name,Location__c,Location__Latitude__s,Location__Longitude__s,Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist, CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_Maintenance_Rating__c,CTC_Breakdowns_Rating__c,CTC_New_Constuction_Rating__c,CTC_Upgrades_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_Maintenance_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')])
            {
                            countList.add(new WrapList(suplist));                           
            }                  
            }
            
            else if(skillset.contains('New Construction'))
            {
                  // fetch supplier with matching values
            for(SCMC__Supplier_Site__c suplist : [select id,Name,Location__c,Location__Latitude__s,Location__Longitude__s, Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist,CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_New_Constuction_Rating__c,CTC_Maintenance_Rating__c,CTC_Breakdowns_Rating__c,CTC_Upgrades_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_New_Constuction_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')])
            {
                            countList.add(new WrapList(suplist));                           
            }    
                
            }
            else if(skillset.contains('Upgrades'))
            {
                  // fetch supplier with matching values
            for(SCMC__Supplier_Site__c suplist : [select id,Name,Location__c,Location__Latitude__s,Location__Longitude__s,Distance(Location__c, GEOLOCATION(:lat,:lon), 'km') dist, CTC_Skill_Set__c,CTC_Product_Type__c,CTC_Product_Brands__c,CTC_High_Lift_Available__c,CTC_Upgrades_Rating__c,CTC_New_Constuction_Rating__c,CTC_Maintenance_Rating__c,CTC_Breakdowns_Rating__c from SCMC__Supplier_Site__c 
                       where CTC_Skill_Set__c includes(:skillset) AND CTC_Product_Brands__c includes(:productbrand) AND CTC_Product_Type__c includes(:producttype) AND CTC_Upgrades_Rating__c>= :complexityrating ORDER BY Distance(Location__c, GEOLOCATION(:lat,:lon), 'km')])
            {
                            countList.add(new WrapList(suplist));                           
            }    
                
            }
                  return countList;
        }
        public class WrapList
        {   
                public SCMC__Supplier_Site__c suCount { get; set; }   
             public Decimal dist {get;set;}
                public WrapList(SCMC__Supplier_Site__c s) 
                {
                    this.suCount = s;
                    dist = (Decimal)s.get('mx');
                }


        }

    }

    I am not able to call the count list in VF Page . Ia m getting the error Error: Unknown property 'GetSupplierListController.WrapList.Id'

<apex:page standardController="SCMC__Requisition__c" extensions="GetSupplierListController" lightningStylesheets="true" >
  
   <apex:pageBlock >
     <apex:pageblocktable value="{!countList}" var="d" columns="9" style="width:900px">
            <apex:column headerValue="Name">
            <apex:outputLink value="/{!d.Id}"><u>{!d.Name}</u></apex:outputLink>
           </apex:column>           
            <apex:column value="{!d.CTC_High_Lift_Available__c}"/>
             <apex:column value="{!d.CTC_Skill_Set__c}"/>
             <apex:column value="{!d.CTC_Product_Type__c}"/>
             <apex:column value="{!d.CTC_Product_Brands__c}"/>
              <apex:column value="{!d.CTC_Breakdowns_Rating__c}"/>
               <apex:column value="{!d.CTC_Maintenance_Rating__c}"/>
                <apex:column value="{!d.CTC_New_Constuction_Rating__c}"/>
                 <apex:column value="{!d.CTC_Upgrades_Rating__c}"/>
                 <apex:outputText value="{!d.dist}"></apex:outputText>
    </apex:pageblocktable>  </apex:pageBlock>
  
</apex:page>
    
      

Please help!
ShivankurShivankur (Salesforce Developers) 
Hi Meenakshi,

I suspect this error could be due to:
The below statement your trying access d as of type WrapList Class.
<apex:outputLink value="/{!d.Id}"><u>{!d.Name}</u></apex:outputLink>
In WrapList class you don't have any property named with id.

You can try replacing Name instead of id.

Try below code which could make sense according to your wrapper class, also try with any other field which could be accessed via Wrapper class, if it doesn't work with Name field:
<apex:outputLink value="{!d.Name}"</apex:outputLink>
Refer to example given on below link to understand how it works with Wrapper class:
https://www.sfdcpoint.com/salesforce/wrapper-class-in-apex/

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.