• WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Implementations
        • Salesforce
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service for SMEs
          • Field Service Lightning
        • Salesforce Partner Apps
          • Panda Doc
          • Conga
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Implementations
      • Salesforce
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service for SMEs
        • Field Service Lightning
      • Salesforce Partner Apps
        • Panda Doc
        • Conga
    • Developments
      • Salesforce Customization
      • Custom Application Development
      • AppExchange Product Development
    • Migrations
      • Classic to Lightning Migration
      • Other Systems to Salesforce Migration
    • Integrations
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to use Address Autocomplete field in LWC using Google API

  • August 18, 2023
  • Tech Blogger
  • Merfantz Developments, Salesforce Developments Tutorial, Salesforce Lightning
  • 0

Description:

An Autocomplete Address feature provides users with the ability to reduce typing time and avoid errors. To utilize this feature, users need to select the address search bar, indicated by a magnifying glass icon, start inputting the address, and then opt for the desired result from the suggested list.

Here’s how the process works:

We will be implementing Address Lookup in Lightning (LWC) using the Google Maps Places API within Lightning Web Components. Users can pick an address from the dropdown, which will subsequently auto-fill address fields based on the selected address. The chosen address can then be saved to the applicable record.

For Lightning Web Components, Salesforce’s Lightning Framework furnishes the lightning-input-address component. This component employs the Google Maps Places API to enable the Address Lookup feature. As the default configuration, all address fields are presented in text format.

To enable Maps and Location Settings in our Salesforce Org, follow these steps:

  1. Enter “Maps” in the Quick Find box and select “Maps and Location Settings.”
  2. Proceed to click “Edit.”
  3. Check the box for “Enable Maps and Location Service” and save the changes.

Please note: Maps and Location Settings are accessible in Professional, Enterprise, Performance, and Unlimited editions, but are not available in the Developer Edition.

Codes:

HTML:

<template>
<div class=”slds-box slds-theme–default”>
<lightning-record-edit-form record-id={recordId} object-api-name={objectAPIName} onsuccess={handleSuccess} onsubmit={handleSubmit}>
<lightning-messages>
</lightning-messages>
<lightning-input-address
address-label=”Address”
street-label=”Street”
city-label=”City”
country-label=”Country”
province-label=”State/ Province”
postal-code-label=”Zip/ Postal Code”
onchange={addressInputChange}
show-address-lookup>
</lightning-input-address>
<lightning-button class=”slds-m-top_small” type=”submit” label=”Update Address”>
</lightning-button>
</lightning-record-edit-form>
</div>
</template>

JavaScript:


import { LightningElement,api } from ‘lwc’;
import { ShowToastEvent } from ‘lightning/platformShowToastEvent’;
export default class AddressLwc extends LightningElement {

@api recordId;
@api objectAPIName;
strStreet;
strCity;
strState;
strCountry;
strPostalCode;
handleSuccess( event ) {
console.log( ‘Updated Record Id is ‘, event.detail.id );
this.dispatchEvent(
new ShowToastEvent({
title: ‘Successful Record Update’,
message: ‘Record Updated Surccessfully!!!’,
variant: ‘success’
})
);
}
handleSubmit( event )

{
let fields = event.detail.fields;
console.log( ‘Fields are ‘ + JSON.stringify( fields ) );
event.preventDefault();
if ( this.objectAPIName === ‘Account’ ) {
fields.BillingStreet = this.strStreet;
fields.BillingCity = this.strCity;
fields.BillingState = this.strState;
fields.BillingCountry = this.strCountry;
fields.BillingPostalCode = this.strPostalCode;
} else if ( this.objectAPIName === ‘Contact’ ) {
fields.MailingStreet = this.strStreet;
fields.MailingCity = this.strCity;
fields.MailingState = this.strState;
fields.MailingCountry = this.strCountry;
fields.MailingPostalCode = this.strPostalCode;
}
this.template.querySelector( ‘lightning-record-edit-form’ ).submit( fields );
}
addressInputChange( event ) {
this.strStreet = event.target.street;
this.strCity = event.target.city;
this.strState = event.target.province;
this.strCountry = event.target.country;
this.strPostalCode = event.target.postalCode;
}
}

JS-Meta.XML:

<?xml version=”1.0″ encoding=”UTF-8″?>
<LightningComponentBundle xmlns=”http://soap.sforce.com/2006/04/metadata”>
<apiVersion>50.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets=”lightning__RecordPage”>
<property name=”objectAPIName” type=”String” label=”Object API Name”/>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>

Output:
Example Image for Autocomplete Address Field

Related Links:
https://merfantz.com/blog/how-to-enable-autocomplete-on-standard-address-fields-in-salesforce

Tags: Autocomplete Addreess Field in Salesdorcehow to create auto complete address field in salesforce using Google apiMerfantz Features
  • Previous Comprehensive Overview of Salesforce Shield
  • Next What is the Significance of Salesforce Testing and Why Is It Necessary?
Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

How to Send a Custom Notification using Flow September 29, 2023
Unlocking Marketing Automation Excellence with Salesforce Pardot | Merfantz September 24, 2023
Looking for the Leading Salesforce Integration Companies? Discover Merfantz September 24, 2023

Copyright @2023 Merfantz Technologies, All rights reserved