Salesforce Certification Days

Salesforce is hosting several complementary 1-day sessions which are designed to help you prepare for your Salesforce certification exam.

As an added bonus, webinar attendees will receive a coupon code for a $100USD discount off any $200USD exam!

Click here to view a list of available sessions. Happy studying!

Work with the Lightning Map Component and Apex Inherited Sharing

This is the hands on challenge from the Platform Developer 1 Winter 19 Certification Maintenance.

UtilityClass

public inherited sharing class UtilityClass {
public static List<sObject> queryObjects(String theObject, List<String> theFields, List<String> theFilters, String sortField, String sortOrder) {
String theQuery = ‘SELECT ‘ + string.join(theFields, ‘,’);
theQuery += ‘ FROM ‘ + theObject;
boolean firstFilter = true;
for (String filter : theFilters) { //loop through the filters
String clauseToUse = (firstFilter) ? ‘ WHERE ‘ : ‘ AND ‘; //get the right clause
filter = filter.trim();
filter = filter.replaceAll(‘(\\s+)’, ‘ ‘); //remove white spaces
theQuery += clauseToUse + filter; //add the filter to the query
firstFilter = false; //changes the filter clause
}
if(!String.isEmpty(sortField)) {
theQuery += ‘ ORDER BY ‘ + sortField;
if(!String.isEmpty(sortOrder)) {
theQuery += ‘ ‘ + sortOrder;
}
}
String theQueryResult = string.escapeSingleQuotes(theQuery); //escapes the string
return database.query(theQueryResult);
}
}

TowerMapController Class

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’};
List<String> theFilters = new List<String>(); //empty filter list
String sortField = ‘Name’;
String sortOrder = ‘ASC’;
List<Tower__c> allTowers = UtilityClass.queryObjects(theObject, theFields, theFilters, sortField, sortOrder);
return allTowers;
}
}

Towermap Lightning Component

<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)}” >
<lightning:map
mapMarkers=”{! v.mapMarkers }”
markersTitle=”{! v.markersTitle }”
zoomLevel=”{!v.zoomLevel}” />
</aura:if>
</aura:component>

Lightning Component Controller

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

Lightning Component 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__Latitude__s,
‘Longitude’ : 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);
component.set(‘v.zoomLevel’,5);
});
$A.enqueueAction(action);
}
})