Sharing is caring!
In today’s data-driven world, gaining an accurate and valid database is strategically essential to improve productivity and enhance business intelligence. With the integration of Salesforce Address Autocomplete and Data 8, it is now easier than ever to make more informed business decisions, increase business ROI and enhance efficiency.
What Is An Autocomplete Address by PostCode?
Autocomplete Address provides various features to write an address by postcode. It also gives a list of addresses related to postcode; the user selects an address and then populates an address field.
Benefits Of Using Autocomplete Address
- It is user-friendly.
- Enable users to find what address they are looking for.
- Help users to spend less time on typing.
- Reduce error on the address.
There are many considerations that should be followed while selecting an autocomplete API – Data8, Postcode, Geocoding API.
What Is Data8?
Data8 offers various innovative tools to help you quickly capture and verify customer data at the point of entry. It comprises a collection of products that includes Address validation, bank validation, Business Insights, Data Cleansing, Name Validation, Email Validation, Phone Validation, and more.
There are many API services for business use cases in data8 such as Address Capture, BankAccount Validation, Email Validation.
Steps To Use Autocomplete Address In Aura Component
Step 1: Create a remote site setting.
Step 2: Create a data8 account and.
Step 3: Generate an API key for Callout.
Empty IP Address and Domains and save.
Step 4: Create PostalCodeApiRequestWrapper Wrapper class for send request
public class PostalCodeApiRequestWrapper {
public String licence{get;set;}
public String postcode{get;set;}
public String building{get;set;}
public options options{get;set;}
public class options{
public String Formatter{get;set;}
public Boolean FixTownCounty{get;set;}
public Integer MaxLines{get;set;}
public Integer MaxLineLength{get;set;}
public Boolean NormalizeCase{get;set;}
public Boolean NormalizeTownCase{get;set;}
public Boolean ExcludeCounty{get;set;}
public Boolean UseAnyAvailableCounty{get;set;}
public String UnwantedPunctuation{get;set;}
public Boolean FixBuilding{get;set;}
public Boolean IncludeUDPRN{get;set;}
public Boolean IncludeLocation{get;set;}
public Boolean ReturnResultCount{get;set;}
public Boolean IncludeNYB{get;set;}
public Boolean IncludeMR{get;set;}
public Boolean IncludeCountry{get;set;}
}
}
Step 5: Create PostalCodeApiResponseWrapper Wrapper class to get a response.
public class PostalCodeApiResponseWrapper {
@auraEnabled public Status Status{get;set;}
@auraEnabled public Integer ResultCount{get;set;}
@auraEnabled public Results[] Results{get;set;}
public class Status{
@auraEnabled public Decimal CreditsRemaining{get;set;}
@auraEnabled public String ErrorMessage{get;set;}
@auraEnabled public Boolean Success{get;set;}
}
public class Results{
@auraEnabled public Address Address{get;set;}
@auraEnabled public RawAddress RawAddress{get;set;}
}
public class RawAddress{
@auraEnabled public String Dps{get;set;}
@auraEnabled public String PostalCounty{get;set;}
@auraEnabled public String PoBox{get;set;}
@auraEnabled public String TraditionalCounty{get;set;}
@auraEnabled public String Postcode{get;set;}
@auraEnabled public String AdministrativeCounty{get;set;}
@auraEnabled public String Locality{get;set;}
@auraEnabled public String CountryISO2{get;set;}
@auraEnabled public String DependentLocality{get;set;}
@auraEnabled public String UniqueReference{get;set;}
@auraEnabled public String DoubleDependentLocality{get;set;}
@auraEnabled public String SubBuildingName{get;set;}
@auraEnabled public String BuildingName{get;set;}
@auraEnabled public Integer BuildingNumber{get;set;}
@auraEnabled public String DependentThoroughfareName{get;set;}
@auraEnabled public String PostcodeType{get;set;}
@auraEnabled public String DependentThoroughfareDesc{get;set;}
@auraEnabled public Integer OrganisationKey{get;set;}
@auraEnabled public String ThoroughfareName{get;set;}
@auraEnabled public Integer AddressKey{get;set;}
@auraEnabled public String ThoroughfareDesc{get;set;}
@auraEnabled public String Department{get;set;}
@auraEnabled public Location Location{get;set;}
@auraEnabled public String Organisation{get;set;}
}
public class Location{
@auraEnabled public Integer Easting{get;set;}
@auraEnabled public String County{get;set;}
@auraEnabled public String CountyCode{get;set;}
@auraEnabled public String DistrictCode{get;set;}
@auraEnabled public Decimal Latitude{get;set;}
@auraEnabled public String District{get;set;}
@auraEnabled public Decimal Longitude{get;set;}
@auraEnabled public String WardCode{get;set;}
@auraEnabled public String GridReference{get;set;}
@auraEnabled public String Ward{get;set;}
@auraEnabled public Integer Northing{get;set;}
@auraEnabled public String Country{get;set;}
}
public class Address{
@auraEnabled public list<String> Lines{get;set;}
}
}
Step 6: Create AutoCompleteAddress_Controller Apex class.
public class AutoCompleteAddress_Controller {
@auraEnabled
Public static PostalCodeApiResponseWrapper PostalCodeAddressRetrieve(String searchpostcode) {
try {
if (String.isNotBlank(searchpostcode)) {
PostalCodeApiRequestWrapper requestWrapper = new PostalCodeApiRequestWrapper();
PostalCodeApiRequestWrapper.options optionObj = new PostalCodeApiRequestWrapper.options();
requestWrapper.licence = ‘FreeTrial’;//Your licence
requestWrapper.postcode = searchpostcode;//PostCode
requestWrapper.building = ”;
optionObj.FixTownCounty = true;
optionObj.MaxLines = 6;
optionObj.MaxLineLength = 255;
optionObj.NormalizeCase = true;
optionObj.NormalizeTownCase = false;
optionObj.ExcludeCounty = false;
optionObj.UseAnyAvailableCounty = false;
optionObj.UnwantedPunctuation = ”;
optionObj.FixBuilding = false;
optionObj.IncludeUDPRN = true;
optionObj.IncludeLocation = true;
optionObj.ReturnResultCount = true;
optionObj.IncludeNYB = false;
optionObj.IncludeMR = false;
requestWrapper.options = optionObj;
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint(‘https://webservices.data-8.co.uk/AddressCapture/GetFullAddress.json?key=Your_API_Key’);// Enter your API Key
req.setHeader(‘Accept’, ‘*/*’);
req.setHeader(‘Accept-Encoding’, ‘gzip, deflate, br’);
req.setHeader(‘Connection’, ‘keep-alive’);
req.setMethod(‘POST’);
req.setBody(JSON.serialize(requestWrapper));
res = http.send(req);
system.debug(res.getBody());
if (res.getStatusCode() == 200) {
PostalCodeApiResponseWrapper responseWrapper = (PostalCodeApiResponseWrapper) JSON.deserialize(res.getBody(), PostalCodeApiResponseWrapper.class);
if (responseWrapper.Results.size() > 0) {
return responseWrapper;
}
}
}
} catch (Exception ex) {
System.debug(‘Error ‘+ ex.getMessage() + ‘Line number’ + ex.getLineNumber());
}
return null;
}
}
Step 7: Create AutoCompleteAddressComp.Controller
({
postalAddress: function (c, e, h) {
var index = e.currentTarget.id;
var searchAddressList = [];
searchAddressList = c.get(“v.addressByPostalCodeList”);
if (!$A.util.isEmpty(searchAddressList)) {
c.set(‘v.addressLine1’, searchAddressList[index].Address.Lines[0]);
c.set(‘v.addressLine2’, searchAddressList[index].Address.Lines[1] + ‘ ‘ + searchAddressList[index].Address.Lines[2] + ‘ ‘ + searchAddressList[index].Address.Lines[3]);
c.set(‘v.city’, searchAddressList[index].RawAddress.Locality);
c.set(‘v.postalCode’, searchAddressList[index].RawAddress.Postcode);
c.set(‘v.addressByPostalCodeList’, []);
}
},
handleKeyUp: function (c, e, h) {
try {
var searchkey = c.get(“v.postalCode”);
var regexp = /^[A-Z]{1,2}[0-9RCHNQ][0-9A-Z]?\s?[0-9][ABD-HJLNP-UW-Z]{2}$|^[A-Z]{2}-?[0-9]{4}$/;
var isvalidPoastalCode = false;
if (regexp.test(searchkey)) {
isvalidPoastalCode = true;
}
if (isvalidPoastalCode) {
if (!$A.util.isEmpty(searchkey) && !$A.util.isEmpty(searchkey.trim())) {
h.handleKeyUp_helper(c, e, h, searchkey);
}
}
else {
c.set(‘v.addressByPostalCodeList’, []);
}
}
catch (error) {
console.error(error);
}
},
}
Step 8: Create AutoCompleteAddressComp.Controller
({
postalAddress: function (c, e, h) {
var index = e.currentTarget.id;
var searchAddressList = [];
searchAddressList = c.get(“v.addressByPostalCodeList”);
if (!$A.util.isEmpty(searchAddressList)) {
c.set(‘v.addressLine1’, searchAddressList[index].Address.Lines[0]);
c.set(‘v.addressLine2’, searchAddressList[index].Address.Lines[1] + ‘ ‘ + searchAddressList[index].Address.Lines[2] + ‘ ‘ + searchAddressList[index].Address.Lines[3]);
c.set(‘v.city’, searchAddressList[index].RawAddress.Locality);
c.set(‘v.postalCode’, searchAddressList[index].RawAddress.Postcode);
c.set(‘v.addressByPostalCodeList’, []);
}
},
handleKeyUp: function (c, e, h) {
try {
var searchkey = c.get(“v.postalCode”);
var regexp = /^[A-Z]{1,2}[0-9RCHNQ][0-9A-Z]?\s?[0-9][ABD-HJLNP-UW-Z]{2}$|^[A-Z]{2}-?[0-9]{4}$/;
var isvalidPoastalCode = false;
if (regexp.test(searchkey)) {
isvalidPoastalCode = true;
}
if (isvalidPoastalCode) {
if (!$A.util.isEmpty(searchkey) && !$A.util.isEmpty(searchkey.trim())) {
h.handleKeyUp_helper(c, e, h, searchkey);
}
}
else {
c.set(‘v.addressByPostalCodeList’, []);
}
}
catch (error) {
console.error(error);
}
},
})
Step 9: Create AutoCompleteAddressComp.Helper
({
handleKeyUp_helper: function (c, e, h, searchkey) {
var action = c.get(“c.PostalCodeAddressRetrieve”);
action.setParams({
“searchpostcode”: searchkey,
});
action.setCallback(this, function (response) {
var responseValue = response.getReturnValue();
var state = response.getState();
if (state === “SUCCESS”) {
if (!$A.util.isEmpty(responseValue)) {
var updatelabel = responseValue.Results;
for(var item of updatelabel){
for(var val of item.Address.Lines){
if(val !== undefined && val !== null && val.length !== 0){
if(item.label !== undefined && item.label !== null && item.label.length !== 0){
item.label = item.label+’ ‘+ val;
}
else{
item.label = val;
}
}
}
}
c.set(‘v.addressByPostalCodeList’, updatelabel);
}
} else {
console.log(‘error’);
}
});
$A.enqueueAction(action);
},
})
Step 10: Create AutoCompleteAddressComp.Css
.THIS .tableClass:hover {
background-color: #D4B168;
}
.THIS .divClass{
padding: 2px !important;
}
Step 11: Create AutoCompleteAddressCompApp
<aura:application extends=”force:slds” >
<c:AutoCompleteAddressComp />
</aura:application>
Salesforce is the one-stop-shop for businesses to easily collaborate with customers, increase employee productivity & efficiency, and boost their bottom line. Companies today are continuously looking for ways to improve while reducing repetitive data entry. Manually entering address data is one user pain point that can be easily solved with Salesforce Autocomplete Address Fields functionality.
With Salesforce Autocomplete Address Fields, Salesforce offers us the ability to quickly lookup and autocomplete addresses that boost up productivity and efficiency of Salesforce Admins. Autocomplete on addresses is available on all standard address fields, across objects including Leads, Contacts, and Accounts.
Deepali Kulshrestha
Salesforce Certified Developer | Delivery Management Head
Deepali, a certified Salesforce Advanced Administrator and Salesforce Developer and CSPO Certified at Cloud Analogy, is a successful name in the industry circles when it comes to the delivery of successful projects with end-to-end testing. Deepali is a globally-renowned industry stalwart when it comes to managing Operations & Delivery Planning in driving Business Performance Management.Hire the best Salesforce Development Company. Choose certified Salesforce Developers from Cloud Analogy now.