Platform Developer I Certification Maintenance (Winter ’19) Work with the Lightning Map Component and Apex Inherited Sharing

TowerMapUtilClass.apxc

public inherited sharing class TowerMapUtilClass {
public static List queryObjects(String theObject, List 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 inherited sharing class TowerMapControllerClass {
@AuraEnabled
public static List getAllTowers() {
String theObject = ‘Tower__c’;
List theFields = new List{‘Id’, ‘Name’, ‘State__r.Name’, ‘Tower_Location__Latitude__s’, ‘Tower_Location__Longitude__s’};
String theFilter = ”;
String sortField = ‘Name’;
String sortOrder = ‘ASC’;
List allTowers = TowerMapUtilClass.queryObjects(theObject, theFields, theFilter, sortField, sortOrder);
return allTowers;
}
}

Towermap.cmp

21 thoughts on “Platform Developer I Certification Maintenance (Winter ’19) Work with the Lightning Map Component and Apex Inherited Sharing”

  1. Hello,

    When i done the activity for the developer part it keeps giving me this error: We can’t find an Apex class named ‘TowerMapUtilClass’.
    Also i didnt find the components available when i tried adding them to the lightning app builder. Can you please help?

    Thanks

  2. I got this error after trying and trying:

    Ensure sharing rules from the calling class are enforced for the the ‘TowerMapUtilClass’ Apex class.

    1. inherited sharing should be missing. Try adding the below for TowerMapUtilClass :

      public inherited sharing class TowerMapUtilClass

    2. Hi

      Just add “public inherited sharing class ” to the Utility class and Tower Map controller code.

      It works 🙂

  3. Not sure how to write in a lightning component . The error says The ‘mapMarkers’ attribute in the ‘Towermap’ component doesn’t have the correct value. Double check the instructions and try again.

  4. Here you go guys 🙂

    Follow 3 Steps :

    1. Open the Developer Console
    2. Create Lightning component named as Towermap and copy the below code to each method.

    ####Towermap.cmp####

    ####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);
    }
    })

    3. Now create new apex class
    ####TowerMapControllerClass.aspx#####

    public inherited sharing class TowerMapControllerClass {
    @AuraEnabled
    public static List getAllTowers() {
    String theObject = ‘Tower__c’;
    List theFields = new List{‘Id’, ‘Name’, ‘State__r.Name’, ‘Tower_Location__Latitude__s’, ‘Tower_Location__Longitude__s’};
    List theFilters = new List(); //empty filter list
    String sortField = ‘Name’;
    String sortOrder = ‘ASC’;
    List allTowers = UtilityClass.queryObjects(theObject, theFields, theFilters, sortField, sortOrder);
    return allTowers;
    }
    }

    hurray done 🙂 🙂

  5. I am getting this error

    Failed to save TowermapHelper.js: c.Towermap: org.auraframework.util.json.JsonStreamReader$JsonStreamParseException: Invalid literal value [1, 1]: ‘public’: Source

  6. Getting this error

    Unable to find action ‘getAllTowers’ on the controller of c:Towermap

    when adding component in App Launcher App

  7. Step 1: Create an object “Tower”

    Step 2:
    Create a lightning Component with name “Towermap” and copy and paste the below code

    <lightning:map
    mapMarkers=”{! v.mapMarkers }”
    markersTitle = “{!v.markersTitle}”
    zoomLevel=”5″ />

    Step 3:
    Create an apex class with name – “Utilityclass”
    and copy-paste the below code

    public inherited sharing class UtilityClass {
    public static List queryObjects(String theObject, List 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);
    }
    }

    Step 4:
    create an apex class – “TowerMapControllerClass ”
    and copy paste the below code
    public inherited sharing class TowerMapControllerClass {
    @AuraEnabled
    public static List getAllTowers() {
    String theObject = ‘Tower__c’;
    List theFields = new List{‘Id’, ‘Name’, ‘State__r.Name’, ‘Tower_Location__Latitude__s’, ‘Tower_Location__Longitude__s’};
    String theFilter =”;
    String sortField = ‘Name’;
    String sortOrder = ‘ASC’;
    List allTowers = UtilityClass.queryObjects(theObject, theFields, theFilter, sortField, sortOrder);
    return allTowers;
    }
    }

    after the above 3 are created, check for completion of the challenge.

    P.S. – I have copied the code provided at the top, but just trying to help anyone with the sequence, if needed

    1. Hi Vatsal,

      Below is the error getting-
      Ensure sharing rules from the calling class are enforced for the the ‘UtilityClass’ Apex class.

      Any idea ?

      Thanks
      Simi

    2. I am getting the below error at Step 2:

      Failed to save Towermap.cmp: c:Towermap:2,12: ParseError at [row,col]:[3,12] Message: Open quote is expected for attribute “mapMarkers” associated with an element type “lightning:map”.: Source

  8. Use this code below:

    1.Create an object named “Tower”.

    2.Create Utility class:

    public inherited sharing class UtilityClass {  
        public static List queryObjects(String theObject, List 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);  
        }  
     }

    3.Create Apex class:TowerMapController

    public inherited sharing class TowerMapController {  
        @AuraEnabled  
        public static List getAllTowers() {  
          String theObject = ‘Tower__c’;  
          List theFields = new List{‘Id’, ‘Name’, ‘State__r.Name’, ‘Tower_Location__Latitude__s’, ‘Tower_Location__Longitude__s’};  
          String theFilter = ”;  
          String sortField = ‘Name’;  
          String sortOrder = ‘ASC’;  
          List allTowers =  UtilityClass.queryObjects(theObject, theFields, theFilter, sortField, sortOrder);  
          return allTowers;  
        }  
     }

    4.Create Towermap Component:-

     
         
         
         
         
           
           
         
     

    5.Controller:-

    ({  
        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);  
        }  
     })

    6.Helper :-

    ({  
        handleInit: function (component, event, helper) {  
          helper.initHelper(component, event, helper);  
          component.set('v.zoomLevel', 5);  
        }  
     })

Leave a Reply

Your email address will not be published. Required fields are marked *