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
MasahiroYMasahiroY 

Accounts plotted on Google Map with Lightning Component

I've successfully plotted the accounts on Google Map with Lightning Component and it works in Sandbox...but don't know how to write a test code for the ApexClass.

I describe the codes below and hope anyone can help with the test code part. Thank you!

Component (MapNearbyAccount.cmp)
<aura:component controller="MapNearbyAccountController" implements="flexipage:availableForAllPageTypes,force:hasRecordId">
    <aura:attribute name="mapMarkers" type="Object"/>
    <aura:attribute name="selectedMarkerValue" type="String" />
    
    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
    
    <div class="slds-box slds-theme--default">
        <lightning:map 
                       mapMarkers="{! v.mapMarkers }"
                       selectedMarkerValue="{!v.selectedMarkerValue}"
                       markersTitle="accounts nearby"
                       listView="auto"
                       showFooter="false"
                       onmarkerselect="{!c.handlerMarkerSelect}" />
    </div>
</aura:component>
Controller (MapNearbyAccount.js)
({
    init: function (cmp, event, helper) {
        var recordId = cmp.get("v.recordId");     
        var action = cmp.get("c.getAccounts");
        action.setParams({recordId :recordId});
        cmp.set('v.mapMarkers', [{location: {}}]);

        action.setCallback(this, function(response){
            
            var accounts = response.getReturnValue();
            var markers = [];
            for(var i = 0; i < accounts.length; i++){
                var acc = accounts[i];
                markers.push({
                    location: {
                        Country : acc.BillingCountry,
                        State : acc.BillingState,
                        City: acc.BillingCity,
                        Street: acc.BillingStreet
                    },
    
                    icon : "standard:account",
                    value: acc.Id,
                    title: acc.Name,
                    description:acc.Description
                });
            }
            
            if(markers.length != 0){
                cmp.set('v.mapMarkers', markers);
            }
        });

        $A.enqueueAction(action);
    },

    handlerMarkerSelect: function (cmp, event, helper) {
        console.log(event.getParam("selectedMarkerValue"));
    }
});
ApexClass (MapNearbyAccountController)
public class MapNearbyAccountController {
    @AuraEnabled
    public static List<Account> getAccounts(String BillingCity, String BillingState, String recordId){
        Account acct = [SELECT Id, Name, BillingCountry, BillingState, BillingCity, BillingStreet, Industry FROM Account WHERE Id =:recordId];
        
        return [SELECT Id, Name, BillingCountry, BillingState, BillingCity, BillingStreet,Description
                FROM Account 
                WHERE BillingState = :acct.BillingState AND BillingCity LIKE :('%' + acct.BillingCity + '%') AND Industry = :acct.Industry LIMIT 10];
    }
}
TestClass
@isTest
public class MapNearbyAccountControllerTest {
@isTest
    static void testMapNearbyAccountController() {
        Account acc1 = new Account();
        acc1.Name='acc1';
        acc1.BillingCity='Shibuya';
        acc1.BillingState='Tokyo';
        insert acc1;
        
        MapNearbyAccountController ctrl = new MapNearbyAccountController();
        
        Test.startTest();
            List<Account> getAccounts = ctrl.getAccounts();
            System.assertEquals(false,getAccounts.isEmpty());
        Test.stopTest();
    }
    
}
Best Answer chosen by MasahiroY
Maharajan CMaharajan C
Hi  Masahiro,

Please use the below test class:
 
@isTest
public class MapNearbyAccountControllerTest {
    @isTest
    static void testMapNearbyAccountController() {
        Account acc1 = new Account();
        acc1.Name='acc1';
        acc1.BillingCity='Shibuya';
        acc1.BillingState='Tokyo';
        insert acc1;
                
        Test.startTest();
        List<Account> getAccounts = MapNearbyAccountController.getAccounts('Shibuya','Tokyo',acc1.Id);
        System.assertEquals(false,getAccounts.isEmpty());
        Test.stopTest();
    }
    
}

Thanks,
Maharajan.C

All Answers

AbhishekAbhishek (Salesforce Developers) 
https://developer.salesforce.com/docs/component-library/bundle/lightning:map/example#lightningcomponentdemo:exampleMapSingleMarker

https://rajvakati.com/2018/09/26/creating-google-maps-with-lightningmap-component/ as this seems to be implementation.


Hope this helps.
Maharajan CMaharajan C
Hi  Masahiro,

Please use the below test class:
 
@isTest
public class MapNearbyAccountControllerTest {
    @isTest
    static void testMapNearbyAccountController() {
        Account acc1 = new Account();
        acc1.Name='acc1';
        acc1.BillingCity='Shibuya';
        acc1.BillingState='Tokyo';
        insert acc1;
                
        Test.startTest();
        List<Account> getAccounts = MapNearbyAccountController.getAccounts('Shibuya','Tokyo',acc1.Id);
        System.assertEquals(false,getAccounts.isEmpty());
        Test.stopTest();
    }
    
}

Thanks,
Maharajan.C
This was selected as the best answer
MasahiroYMasahiroY
@Maharajan C thanks for your test code. I'm super happy it worked perfectly ;)
ShannuShannu
@MasahiroY Will the data Plot on Map as per the List view records? Please confirm.
ShannuShannu
How to write test class for following code.
public class LightningMapCntrl {

    Public String accountIds{get;set;}
    public LightningMapCntrl(ApexPages.StandardSetController cntlr){
        List<Account>selAccounts = cntlr.getSelected(); //get selected records from account list view
        accountIds = ''; 
        for(Account acc : selAccounts){
            accountIds += acc.Id + '-'; //build list of ids string concatenated with comma                         
        }
        accountIds = accountIds.removeEnd('-'); 
        system.debug('+++ IDs : ' + accountIds) ;
    } 
    @auraEnabled
    public static List<Account> getAccountList(String accountIds){
        system.Debug('++++ accountIds : ' + accountIds);
        List<String> accountIdList = accountIds.split('-');
        return [Select Id, Name, type , BillingCity, BillingStreet,BillingPostalCode,BillingCountry, BillingState from account where Id In: accountIdList ]; 
    }
    
}
Sai Shanmukh 15Sai Shanmukh 15
How to display stagename or other fields on maps using lwc.
Currently, i am displaying only opportunity name and marker on map using location (Address fields salesforce). But how to display stagename and other fields on maps). Please advise.
User-added image
Satyam Singh 49Satyam Singh 49

nice 

Click here to check latest New sarkari Yojana (https://newsarkariyojna.site/)