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
Janno RipJanno Rip 

Create a list of accounts and access their fields

Hello developers,

I am tackling a problem which is probably quite straight forward but I can't figure it out:

I have extend the code from https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_maps_example.htm to fit my needs.

In there I have 
public List<Map<String,Double>> locations { get; private set; }
which later gets reffered to here:
// SOQL query to get the nearest warehouses

        String queryString =
           'SELECT Id, Name,AD_JobAds_Anz__c,Vollst_ndiger_Name__c,ShippingStreet,LocateCity__longitude__s, LocateCity__latitude__s  ' +
           'FROM Account ' +
           'WHERE DISTANCE(LocateCity__c, GEOLOCATION('+lat+','+lon+'), \'km\') <  3 and Kundenstatus_Direktvertrieb__c = \'Bestandskunde\' and Id != :theaccId ' +
           'ORDER BY DISTANCE(LocateCity__c, GEOLOCATION('+lat+','+lon+'), \'km\') '; 
        
        List <Account> warehouses = database.Query(queryString);
                 
        if(0 < warehouses.size()) {
            // Convert to locations that can be mapped
            locations = new List<Map<String,Double>>();
            for (Account wh : warehouses) {
                locations.add(
                    new Map<String,Double>{
                        'latitude' => wh.LocateCity__latitude__s, 
                        'longitude' => wh.LocateCity__longitude__s

Now I am trying to access the fields/values of the account but since it is a List of Maps, this is not working. I have to tranform it to a list that contains the Accounts/sObject?? Then I can access its fields?

Maybe some can share some insights.
Thanks!


 

Best Answer chosen by Janno Rip
Janno RipJanno Rip
After some more trial and error I solved this one myself.
 
public List<Account> warehouses {get; private set;}
 
warehouses = [ SELECT Id, Name,URL_zum_CC__c,Auftragseingangstyp_picklist__c,AD_MS_Rel_Anzahl_bez__c,Vollst_ndiger_Name__c,ShippingStreet,LocateCity__longitude__s, LocateCity__latitude__s, WZ_Buchstabe__c,OwnerId,GeoLocPosition__c,Accountinhaber_Text__c,WirtschaftszweigWZ08__c,AE_letzte_12_Monate__c,anzeigendaten_de_ID_account__c,indexurl__c FROM Account WHERE Letzte_gew_Opp_OneSales_in_Tagen__c <= :lastDeal AND DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') < :maxDistance AND Id != :theaccId AND WZ_Buchstabe__c = :currentAccount.WZ_Buchstabe__c ORDER BY DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') LIMIT :recordLimit ];

Then in my VF Page:
 
<apex:repeat value="{!warehouses}" var="war"> <apex:mapMarker position="{!war.GeoLocPosition__c}" title="{!war.Name}"


GeoLocPosition__c is formula field that allows me to get the latitude and longitude in the same format as the "locations" Map did:

"{latitude="&TEXT(LocateCity__Latitude__s)&",longitude="&TEXT(LocateCity__Longitude__s)&"}"

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Janno,

Were you getting any errors or were you getting different results than expected?

Regards,
Anutej
Janno RipJanno Rip

Hello Anutej,

thank you for your reply. It seems like I did not emphazise enough on my actual goal. In my Visualforce Page I am trying to access the accounts I got from my query. As for now it looks like this

<apex:repeat value="{!locations}" var="pos">
                    <apex:mapMarker title="test1"> 

User-added image

I want to be:
<apex:repeat value="{!locations}" var="pos">
                    <apex:mapMarker title="{!pos.Name}">
so that for every Marker the actual accountname will get displayed.

This is not working because 'locations' does not allow me to access the accounts field. Unfortunately I am  just modifying existing code and am not a developer myself. I was told that locations is a list of Maps but I need to make it a list of accounts to access to the geoloc plus account fields.

I am pretty lost with this part here:
if(0 < warehouses.size()) {
            // Convert to locations that can be mapped
            locations = new List<Map<String,Double>>();
            for (Account wh : warehouses) {
                locations.add(
                    new Map<String,Double>{
                        'latitude' => wh.LocateCity__latitude__s, 
                        'longitude' => wh.LocateCity__longitude__s

 
Janno RipJanno Rip
After some more trial and error I solved this one myself.
 
public List<Account> warehouses {get; private set;}
 
warehouses = [ SELECT Id, Name,URL_zum_CC__c,Auftragseingangstyp_picklist__c,AD_MS_Rel_Anzahl_bez__c,Vollst_ndiger_Name__c,ShippingStreet,LocateCity__longitude__s, LocateCity__latitude__s, WZ_Buchstabe__c,OwnerId,GeoLocPosition__c,Accountinhaber_Text__c,WirtschaftszweigWZ08__c,AE_letzte_12_Monate__c,anzeigendaten_de_ID_account__c,indexurl__c FROM Account WHERE Letzte_gew_Opp_OneSales_in_Tagen__c <= :lastDeal AND DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') < :maxDistance AND Id != :theaccId AND WZ_Buchstabe__c = :currentAccount.WZ_Buchstabe__c ORDER BY DISTANCE(LocateCity__c, GEOLOCATION(:dlat,:dlon), 'km') LIMIT :recordLimit ];

Then in my VF Page:
 
<apex:repeat value="{!warehouses}" var="war"> <apex:mapMarker position="{!war.GeoLocPosition__c}" title="{!war.Name}"


GeoLocPosition__c is formula field that allows me to get the latitude and longitude in the same format as the "locations" Map did:

"{latitude="&TEXT(LocateCity__Latitude__s)&",longitude="&TEXT(LocateCity__Longitude__s)&"}"
This was selected as the best answer