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
niharnihar 

Work with the Lightning Map Component and Apex Inherited Sharing

Hi All,
        Can Anyone Provide me the Apex codes for the above winter 19 pd1 maintainces badge exam.......

Thanks in advance.....................
Best Answer chosen by nihar
anshul saini 7anshul saini 7
Towermap.cmp
<aura:component implements="flexipage:availableForAllPageTypes" controller="TowerMapControllerClass" access="global" >
     <aura:attribute name="mapMarkers" type="Object" access="PRIVATE" />
     <aura:attribute name="markersTitle" type="String" access="PRIVATE"/>
     <aura:handler name="init" value="{!this}" action="{!c.handleInit}"/>
     <aura:if isTrue="{!!empty(v.mapMarkers)}" >
          <!-- Create lightning:map here -->
         <lightning:map 
         mapMarkers="{! v.mapMarkers }" 
         markersTitle="{! v.markersTitle}"
        zoomLevel="5" />
     </aura:if>
</aura:component>

TowerMapUtilClass.apxc
public inherited sharing class TowerMapUtilClass {
     public static List<sObject> queryObjects(String theObject, List<String> theFields, String theFilter, String sortField, String sortOrder) {
          String theQuery = 'SELECT ' + string.join(theFields, ',');
          theQuery += ' FROM ' + theObject;
          if(!String.isEmpty(theFilter)) {
               theQuery += ' WHERE ' + theFilter;
          }
          if(!String.isEmpty(sortField)) {
               theQuery += ' ORDER BY ' + sortField;
               if(!String.isEmpty(sortOrder)) {
                    theQuery += ' ' + sortOrder;
               }
          }
          return database.query(theQuery);
     }
}

TowerMapControllerClass.apxc
public with sharing class TowerMapControllerClass {
     @AuraEnabled
     public static List<Tower__c> getAllTowers() {
          String theObject = 'Tower__c';
          List<String> theFields = new List<String>{'Id', 'Name', 'State__r.Name', 'Tower_Location__Latitude__s', 'Tower_Location__Longitude__s'};
          String theFilter = '';
          String sortField = 'Name';
          String sortOrder = 'ASC';
          List<Tower__c> allTowers = TowerMapUtilClass.queryObjects(theObject, theFields, theFilter, sortField, sortOrder);
          return allTowers;
     }
}

Towermapcontroller.js
({
     handleInit: function (component, event, helper) {
          helper.initHelper(component, event, helper);
     }
})

TowermapHelper.js
({
     initHelper : function(component, event, helper) {
          helper.utilSetMarkers(component, event, helper);
     },
     utilSetMarkers : function(component, event, helper) {
          let action = component.get("c.getAllTowers");
          action.setCallback(this, function(response) {
               const data = response.getReturnValue();
               const dataSize = data.length;
               let markers = [];
               for(let i=0; i < dataSize; i += 1) {
                    const Tower = data[i];
                    markers.push({
                        'location': {
                             'Latitude' : Tower.Tower_Location__Latitude__s,
                             'Longitude' : Tower.Tower_Location__Longitude__s
                        },
                        'icon': 'utility:Tower',
                        'title' : Tower.Name,
                        'description' : Tower.Name + ' Tower Location at ' + Tower.State__r.Name
                   });
               }
               component.set('v.markersTitle', 'Out and About Communications Tower Locations');
               component.set('v.mapMarkers', markers);
          });
          $A.enqueueAction(action);
     }
})

this code is wrking correctly for (Work with the Lightning Map Component and Apex Inherited Sharing).
 

All Answers

anshul saini 7anshul saini 7
Towermap.cmp
<aura:component implements="flexipage:availableForAllPageTypes" controller="TowerMapControllerClass" access="global" >
     <aura:attribute name="mapMarkers" type="Object" access="PRIVATE" />
     <aura:attribute name="markersTitle" type="String" access="PRIVATE"/>
     <aura:handler name="init" value="{!this}" action="{!c.handleInit}"/>
     <aura:if isTrue="{!!empty(v.mapMarkers)}" >
          <!-- Create lightning:map here -->
         <lightning:map 
         mapMarkers="{! v.mapMarkers }" 
         markersTitle="{! v.markersTitle}"
        zoomLevel="5" />
     </aura:if>
</aura:component>

TowerMapUtilClass.apxc
public inherited sharing class TowerMapUtilClass {
     public static List<sObject> queryObjects(String theObject, List<String> theFields, String theFilter, String sortField, String sortOrder) {
          String theQuery = 'SELECT ' + string.join(theFields, ',');
          theQuery += ' FROM ' + theObject;
          if(!String.isEmpty(theFilter)) {
               theQuery += ' WHERE ' + theFilter;
          }
          if(!String.isEmpty(sortField)) {
               theQuery += ' ORDER BY ' + sortField;
               if(!String.isEmpty(sortOrder)) {
                    theQuery += ' ' + sortOrder;
               }
          }
          return database.query(theQuery);
     }
}

TowerMapControllerClass.apxc
public with sharing class TowerMapControllerClass {
     @AuraEnabled
     public static List<Tower__c> getAllTowers() {
          String theObject = 'Tower__c';
          List<String> theFields = new List<String>{'Id', 'Name', 'State__r.Name', 'Tower_Location__Latitude__s', 'Tower_Location__Longitude__s'};
          String theFilter = '';
          String sortField = 'Name';
          String sortOrder = 'ASC';
          List<Tower__c> allTowers = TowerMapUtilClass.queryObjects(theObject, theFields, theFilter, sortField, sortOrder);
          return allTowers;
     }
}

Towermapcontroller.js
({
     handleInit: function (component, event, helper) {
          helper.initHelper(component, event, helper);
     }
})

TowermapHelper.js
({
     initHelper : function(component, event, helper) {
          helper.utilSetMarkers(component, event, helper);
     },
     utilSetMarkers : function(component, event, helper) {
          let action = component.get("c.getAllTowers");
          action.setCallback(this, function(response) {
               const data = response.getReturnValue();
               const dataSize = data.length;
               let markers = [];
               for(let i=0; i < dataSize; i += 1) {
                    const Tower = data[i];
                    markers.push({
                        'location': {
                             'Latitude' : Tower.Tower_Location__Latitude__s,
                             'Longitude' : Tower.Tower_Location__Longitude__s
                        },
                        'icon': 'utility:Tower',
                        'title' : Tower.Name,
                        'description' : Tower.Name + ' Tower Location at ' + Tower.State__r.Name
                   });
               }
               component.set('v.markersTitle', 'Out and About Communications Tower Locations');
               component.set('v.mapMarkers', markers);
          });
          $A.enqueueAction(action);
     }
})

this code is wrking correctly for (Work with the Lightning Map Component and Apex Inherited Sharing).
 
This was selected as the best answer
Ramadhar Mishra 24Ramadhar Mishra 24
These are correct answer!
Lisette GomezLisette Gomez
Thanks!!! The code is correct!!
anshul saini 7anshul saini 7
if code is working for you, Please choose it for Best Answer
 
Ipsita Mohapatra 5Ipsita Mohapatra 5
i have added same code but I am getting below error when I try to add my component in App

Uncaught Error in $A.getCallback() [Cannot read property 'length' of null]
Callback failed: apex://TowerMapControllerClass/ACTION$getAllTowers
 
Ipsita Mohapatra 5Ipsita Mohapatra 5
@Anshul - Can you pls advice
satish kumar 235satish kumar 235
I am getting the below error when i used your code.
Please help me out....
Error in $A.getCallback() [Cannot read property 'length' of null]
Callback failed: apex://TowerMapControllerClass/ACTION$getAllTowers
satish kumar 235satish kumar 235
                                                           This is error
                                                           Sorry to interrupt
This page has an error. You might just need to refresh it. Error in $A.getCallback() [Cannot read property 'length' of null] Callback failed: apex://TowerMapControllerClass/ACTION$getAllTowers Failing descriptor: {c:Towermap}
Ipsita Mohapatra 9Ipsita Mohapatra 9
Hi Satish , Pls check the column names in your SOQL Queries. I was same issue but got past it when I compared the tower__c column names in the code with the names specific to database of my org using developer console/workbench/eclipse etc
satish kumar 235satish kumar 235




User-added image

Hi Mahapatra,
i have modified my queries by fieldnames and object name by  copying in workbench.
My code is now working but problem is map is not displaying proprly.. Can you please check it out?
Ipsita Mohapatra 9Ipsita Mohapatra 9
Pls find my codes below 
 
<aura:component implements="flexipage:availableForAllPageTypes" controller="TowerMapController" access="global" >
     <aura:attribute name="mapMarkers" type="Object" access="PRIVATE" />
     <aura:attribute name="markersTitle" type="String" access="PRIVATE" />
     <aura:handler name="init" value="{!this}" action="{!c.handleInit}"/>
     <aura:if isTrue="{!!empty(v.mapMarkers)}" >
          <!-- Create lightning:map here -->
          <lightning:map markersTitle = "{! v.markersTitle }"   mapMarkers="{! v.mapMarkers }"  zoomLevel="5" />
     </aura:if>
</aura:component>
 
({
     handleInit: function (component, event, helper) {
          helper.initHelper(component, event, helper);
     }
})
 
({
     initHelper : function(component, event, helper) {
          helper.utilSetMarkers(component, event, helper);
     },
     utilSetMarkers : function(component, event, helper) {
          let action = component.get("c.getAllTowers");
          action.setCallback(this, function(response) {
               const data = response.getReturnValue();
               const dataSize = data.length;
               let markers = [];
               for(let i=0; i < dataSize; i += 1) {
                    const Tower = data[i];
                    markers.push({
                        'location': {
                             'Latitude' : Tower.Location__Longitude__s,
                             'Longitude' : Tower.Location__Latitude__s
                        },
                        'icon': 'utility:Tower',
                        'title' : Tower.Name,
                        'description' : Tower.Name + ' Tower Location at ' + Tower.State__r.Name
                   });
               }
               component.set('v.markersTitle', 'Out and About Communications Tower Locations');
               component.set('v.mapMarkers', markers);
          });
          $A.enqueueAction(action);
     }
})
public inherited sharing class UtilityClass {
     public static List<sObject> queryObjects(String theObject, List<String> theFields, String theFilter, String sortField, String sortOrder) {
          String theQuery = 'SELECT ' + string.join(theFields, ',');
          theQuery += ' FROM ' + theObject;
          if(!String.isEmpty(theFilter)) {
               theQuery += ' WHERE ' + theFilter;
          }
          if(!String.isEmpty(sortField)) {
               theQuery += ' ORDER BY ' + sortField;
               if(!String.isEmpty(sortOrder)) {
                    theQuery += ' ' + sortOrder;
               }
          }
          return database.query(theQuery);
     }
}
 
public inherited sharing class TowerMapController {

    @AuraEnabled
     public static List<Tower__c> getAllTowers() {
          String theObject = 'Tower__c';
          List<String> theFields = new List<String>{'Id', 'Name', 'State__r.Name', 'Location__Latitude__s', 'Location__Longitude__s'};
          String theFilter = '';
          String sortField = 'Name';
          String sortOrder = 'ASC';
          List<Tower__c> allTowers = UtilityClass.queryObjects(theObject, theFields, theFilter, sortField, sortOrder);
          return allTowers;
     }
}

Please find below SOQL from my org database
SELECT Id, IsDeleted, Name, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, LastViewedDate, LastReferencedDate, Location__Latitude__s, Location__Longitude__s, Location__c, State__c FROM Tower__c