3

Does anyone have any example Apex or Salesforce Lightning JavaScript code for doing a Postcode to Addresses lookup using the getaddress.io REST API within Salesforce Lightning?

Specifically how to performing a GET request to end point:

https://api.getAddress.io/find/{postcode}?api-key={api-key}

And handle the JSON response of:

{
    "latitude": 52.24593734741211,
    "longitude": -0.891636312007904,
    "addresses":["Line1,Line2,Line3,Line4,Locality,Town/City,County"]
}

As per their documentation

Either client-side Salesforce Lightning or Server-side Apex example code/classes would be great.

  • I've answered with my apex code in an attempt to be a good citzen and give back to the community, as per Joel Spolsky post: stackoverflow.blog/2012/05/22/encyclopedia-stack-exchange and the Salesforce Stack Exchange Help Center article 'Can I answer my own question?' salesforce.stackexchange.com/help/self-answer - where it seems we are being encouraged to contribute in this way... – Robs Jun 5 '18 at 14:57
  • 1
    I've added my vote to reopen. Note that in the review queue, only the question is shown by default, so it's not immediately clear that you are actually providing the sample code. – David Reed Jun 5 '18 at 15:25
  • Does the quality of this question still warrant being 'on hold' ? – Robs Jun 6 '18 at 14:10
  • Honestly if it were just the question, this post would still be off topic. The answer redeems it, however. – Adrian Larson Jun 25 '18 at 23:46
5

I've taken the server-side Apex approach to integrating with their REST API.

Usage

String postcode = 'NG16 2TH';
GetAddressAPI api = new GetAddressAPI()
String result = api.getAddresses(postcode);

Apex

public class GetAddressAPI {

    // Get Address API: https://getaddress.io/
    // Documentation: https://getaddress.io/Documentation

    public String getAddresses(String postcode) {

        if (postcode == null) {
            return null;
        }

        string responseData = makeAPICall(postcode);

        System.debug(responseData);

        if (responseData == null){
            return null;
        }

        LookupAddressResponse response = (LookupAddressResponse)JSON.deserialize(responseData, LookupAddressResponse.class);      

        List<LookupAddress> addresses = new List<LookupAddress>();

        for(string a : response.addresses){

            String[] segments = a.split(',');

            LookupAddress address = new LookupAddress();
            address.latitude = response.latitude;
            address.longitude = response.longitude;
            address.line1 = segments[0].trim();
            address.line2 = segments[1].trim();
            address.line3 = segments[2].trim();
            address.line4 = segments[3].trim();
            address.locality = segments[4].trim();
            address.townCity = segments[5].trim();
            address.county = segments[6].trim();
            address.postcode = postcode;

            String fullAddress = '';
            for (string segment : segments){
                if (String.isNotBlank(segment)){
                    if (fullAddress != ''){
                        fullAddress += ', ';
                    }
                    fullAddress += segment;
                }
            }
            fullAddress += ', ' + postcode;
            address.FullAddress = fullAddress;
            addresses.add(address);
        }

        return JSON.serialize(addresses);
    }

    public string makeAPICall(string postcode) {

        if (postcode == null) {
            return null;
        }        

        String url = 'https://api.getAddress.io/find/'
            + EncodingUtil.urlEncode(postcode.replace(' ', ''), 'UTF-8')
            + '?api-key=' + getAPIKey();

        Http http = new Http();
        HttpRequest request = new HttpRequest();
        HttpResponse httpResponse = new HttpResponse();
        request.setMethod('GET');
        request.setEndpoint(url);
        request.setTimeout(120000);
        httpResponse = http.send(request); 

        if (httpResponse.getStatusCode() == 200){
            return httpResponse.getBody();    
        } else {
            System.debug(httpResponse.getStatusCode());
            System.debug(httpResponse.getBody());
            throw new AuraHandledException(httpResponse.getBody());
        }        
    }

    private String getAPIKey() {
        return ''; // Add your API key or get it from custom settings
    }

    class LookupAddressResponse {
        Decimal latitude;
        Decimal longitude;
        List<String> addresses;
    }

    class LookupAddress {
        Decimal latitude;
        Decimal longitude;
        String fullAddress;
        String line1;
        String line2;
        String line3;
        String line4;
        String locality;
        String townCity;
        String county;
        String postcode;
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.