1

I have created a Lightning component to display nearBy accounts using Geolocation for a given Account. I am able to display a list of locations passing BillingLatitide and BillingLongitude from the current Account using Lightning Data Service and using an Apex Controller to query nearby Accounts. The problem is when I am in an Account page, And Edit thh Address and Save it, I get a error message, Even though after the Edit, component shows the correct list of Accounts. I believe I need to use Lightning Events or need to handle Onchange attribute in within the Controller, but I am not able to figure this out.

NearByAccounts.cmp

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickActionWithoutHeader" access="global" controller="NearbyAccounts">
<aura:attribute name="recordId" type="Id"/>
<aura:attribute name="account" type="Account"/>
<aura:attribute name="nearbyAccounts" type="Object[]"/>



<force:recordData recordId="{!v.recordId}"
                  targetRecord="{!v.account}"
                  targetFields="{!v.simpleAccount}"
                  fields="Id, Name, BillingLatitude, BillingLongitude"

                  recordUpdated="{!c.doInit}"/>
<lightning:card iconName="standard:account" title="Nearby Accounts" class="slds-is-relative">
    <div class="slds-p-left_medium slds-p-right_medium">
        <ul>
        <aura:if isTrue="{!v.nearbyAccounts.length &gt; 0}">
            <aura:iteration items="{!v.nearbyAccounts}" var="acc">
                <li class="slds-list__item">
                    <c:NearbyAccount account="{!acc}"/>
                </li>

            </aura:iteration>
            <aura:set attribute="else">
                <li class="slds-list__item">
                    <h3 class="slds-text-small slds-text-color_error">No nearby Accounts found.</h3>
                </li>
            </aura:set>
        </aura:if>
        </ul>
    </div>
</lightning:card>

NearByAccounts.js

({
    doInit : function(component, event, helper) {
        var action = component.get("c.getNearbyAccounts");
        action.setParams({
            latitude: component.get("v.account.fields.BillingLatitude.value"),
            longitude: component.get("v.account.fields.BillingLongitude.value")
        });
        action.setCallback(this, function(response){
            var nearByAccounts = response.getReturnValue();
            component.set("v.nearbyAccounts", nearByAccounts);

        });
        $A.enqueueAction(action);

    }
})

NearbyAccount.cmp

<aura:component >
    <aura:attribute name="account" type="Account"/>
    <lightning:recordViewForm aura:id="viewForm" recordId="{!v.account.Id}" objectApiName="Account">
        <div class="slds-tile__detail">
        <div class="slds-grid slds-grid_align-spread slds-has-flexi-truncate">
            <a onclick="{!c.navToRecord}" >
                <h3  class="slds-tile__title slds-truncate" title="Salesforce UX">

                    {!v.account.Name} 
                    (<lightning:formattedNumber value="{!v.account.dist}"/> mi)

                </h3>
            </a>
        </div>

    </div>
    </lightning:recordViewForm>


</aura:component>

NearByAccount.js

({
     navToRecord : function (component, event, helper) {
        var navEvt = $A.get("e.force:navigateToSObject");
        navEvt.setParams({
            "recordId": component.get("v.account.Id")
        });
        navEvt.fire();
    },
})

enter image description here

Adding the apex class method

public static List<Account> getNearbyAccounts(Decimal latitude, Decimal longitude){
    System.debug('Latitude: '+latitude);
    System.debug('Longitude: '+longitude);
    List<Account> accList = [SELECT Id, Name, BillingLatitude, ParentId, BillingLongitude, BillingCity, BillingState, BillingPostalCode, BillingCountry, BillingStreet,
                             Distance(BillingAddress, GeoLocation(:latitude, :longitude),'mi') dist
                             FROM Account
                            WHERE Distance(BillingAddress, GeoLocation(:latitude, :longitude),'mi') < 5
                            AND Distance(BillingAddress, GeoLocation(:latitude, :longitude),'mi') > 0];
  for ( Account acct : accList ) {
     System.debug( acct.get('dist' ) );
  }
    return accList; 

}
  • Can you log the getReturnValue() when you perform the update, as well as the outgoing parameters? You aren't checking the result status of the Apex call, and I wonder if the data is not taking the shape your component expects. – David Reed Oct 11 '18 at 13:29
  • Additionally to @DavidReed comment, please add the apex getNearbyAccounts method code to your question – Itai Shmida Oct 25 '18 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.