Platform Developer I Certification Maintenance (Winter '19) Trailhead Challenge
After Creating the Tower object in the developer org add the four records mentioned in the trailhead challenge and copy the below code blocks to finish the challenge.
- Copy the helper class code block, TowerMapUtilClass, from above and modify it so the sharing rules are enforced based on the sharing setting of the calling class.
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); } }
- Copy the controller code block, TowerMapControllerClass, from above and modify it so the sharing rules are enforced.
public inherited 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; } }
- Copy and modify the Lightning component code above to implement a
lightning:map
component named Towermap. Replace thewith your code. Your code should pass the value of
v.mapMarkers
to themapMarkers
attribute, pass the value ofv.markersTitle
to themarkersTitle
attribute, and set the zoomLevel attribute to 5. <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 }" zoomLevel="{!v.zoomLevel}" markersTitle="{!v.markersTitle}"/> </aura:if> </aura:component>
- Copy and paste the controller and helper code blocks above to create controller and helper functions to pull all Tower locations from the server.
({ handleInit: function (component, event, helper) { helper.initHelper(component, event, helper); component.set('v.zoomLevel', 5); } })
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); } })
- To view your map, you can use Lightning App Builder to create a standalone one-column app page named Towers and add it to the Lightning Experience App Launcher. More experienced developers can chose another method to expose the component, if desired.
- Setup ➤ Lightning App Builder ➤ Click "New" ➤ Select App Page ➤ Click Next ➤ Give Label as "Towers" ➤ Select "One Column" ➤ Select "Towermap" under the lightning components section and add the component to the lightning page
- Save and activate the lightning page to display the page in the sales,service applications.
TowerMapUtilClass
TowerMapControllerClass
Towermap.cmp
TowermapController.js
All the best..!😉