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
saurabh8singhsaurabh8singh 

postcode lookup field.

Hi All
  I have a requirement of creating a PostCode field on lightning experience. Whenever anyone enters a 4 digit Australian postcode into this field, it should be able to pull suburb name matching to that postcode. There are more than 3k suburbs in a single state.
I can thought of a solution where I need to write an GET API to Australian Post (Auspost provides free api for search based on postcodes) which will return a JSON response having suburb details. 
Can anyone provide their thoughts on this requirement or if anyone has worked on such requirement.
Any help on this is much appreciated.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
You could implement a callout to the postal service, but that would introduce a delay in the user experience.  And if you need to update suburb information in a trigger or process builder, you'll need to make the call asynchronously (via @future or Queueable method).

With 4 digits, there's a maximum of 10,000 possible postal codes.  This used to be a use case for Custom Settings, but Salesforce is now discouraging the use of List Custom Settings in favor of Custom Metadata records.  So, if you can obtain a complete list of postal code / suburb data, you could define a simple Custom Metadata Type and create 10,000 (or however many there actually are) records.  The advantage to Custom Metadata is that you can deploy it (via Change Set or other deployment methods), unlike data, which has to be loaded with Dataloader or the Import Wizard.  And it doesn't count against data storage.

The other option is to create a Custom Object and import 10,000 records.  That will eat 20 MB of data storage, and the data will need to be loaded into sandboxes everytime they're created / refreshed.

Finally, the callout will always be up-to-date.  The metadata will have to be updated periodically as postal codes change.  Which raises a question: if a suburb's postal code changes, how will you update existing records?
Raj VakatiRaj Vakati
As you said , you can make an API call from the lightning component and implement datatable to show the relavent data from the JSON 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
You could also store subsets of the postal code data in Custom Metadata records.  For example, create 100 records, one for each combination of the first two digits.  Then store data for 100 suburbs as serialized JSON in a Long Text field in each record.  Just a thought...
saurabh8singhsaurabh8singh
Hi Glyn
  Thanks for your reply. Even I thought of creating custom metadata and use that but how to create 10000 records in the metadata object. Salesforce does not allow it using dataloader I guess. Any thoughts on creating 10000 records ??
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Indeed!  In my DE org, I created a Custom Object called TempPostalCodeInfo__c with a Suburb__c field, so I'm using the Name field for the postal code and the Suburb__c field to describe the corresponding location.  I created a Custom Metadata Type called PostalCodeInfo__mdt with a Suburb__c field.  In this case, the Label field will hold the postal code.  Then, I manually created 3 TempPostalCodeInfo__c records (but in your case, you could dataload 10,000 of them), and ran the code below in the Anonymous Execution window to convert them to PostalCodeInfo__mdt records and deploy them.

Before:
User-added image

The code:
Metadata.DeployContainer postalCodes = new Metadata.DeployContainer();

for ( TempPostalCodeInfo__c info : [SELECT Name, Suburb__c FROM TempPostalCodeInfo__c] )
{
    Metadata.CustomMetadataValue suburb = new Metadata.CustomMetadataValue();
    suburb.field = 'Suburb__c';
    suburb.value = info.Suburb__c;

    Metadata.CustomMetadata postalCode = new Metadata.CustomMetadata();
    postalCode.fullName = 'PostalCodeInfo__mdt.PC_' + info.Name;
    postalCode.label = info.Name;
    postalCode.values = new List<Metadata.CustomMetadataValue>{ suburb };

    postalCodes.addMetadata( postalCode );
}

Metadata.Operations.enqueueDeployment( postalCodes, null );

After:
User-added image

Let me know if you think this could work for you.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
One more thing -- The idea is to delete the TempPostalCodeInfo__c object when you're done.