Customer addresses are an essential part of any business that deals with sales, delivery, invoicing, or marketing. Having accurate and consistent customer addresses can help improve customer satisfaction, reduce operational costs, and optimize business processes. However, maintaining customer address data can be challenging, especially when dealing with different formats, languages, and standards across countries and regions. One way to simplify and automate the process of validating customer addresses is to use the Google Address Validation API. This is a web service that provides address validation and standardization for over 240 countries and territories. The API can verify if an address exists, correct spelling and formatting errors, and return a standardized address that follows the official postal service rules.
This article will show users how to use this API to validate customer addresses from their master data in SAP.
Master data is the core data that is used as a base for any transaction. Customer master data is one of the most important data in SAP, as it contains information about customers such as name, address, contact details, payment terms, etc.
We will create a report in ABAP, that will read customer addresses from a table, call Google Address Validation API for each address, check the response status, and write the result.
The benefits of using this API for address validation are:
- It can handle complex and ambiguous addresses that may not follow a standard format or structure.
- It can provide a standardized and consistent address format that follows the official postal service conventions.
- It can enrich the address data with additional information such as geographic coordinates, postal code, country code, etc.
- It can improve the quality and accuracy of your customer address data and reduce errors and duplicates.
Prerequisites
Before following this article, you need to have the following requirements and assumptions:
- You have access to an SAP system with ABAP Workbench tools.
- You have basic knowledge of ABAP programming and SAP tables.
- You have a Google account and a valid API key for Google Address Validation API
- You have enabled the Google Address Validation API for your project in Google Cloud Console.
- You have an internet connection and permission to access external web services from your SAP system.
To obtain an API key from Google and enable the Google Address Validation API, you can follow these steps:
- Go to https://console.cloud.google.com/ and sign in with your Google account. When you create your account, you get 300usd credit to explore the solutions.
- Create a new project or select an existing one
- Go to APIs & Services > Library and search for “Address Validation API”
- Click on Enable to activate the API for your project
- After enabling it, you can get the Credentials. Click on Create Credentials > API key
- Copy the generated API key and keep it safe (don’t share it)
Now that we have the API, we can start coding. To create a report in ABAP and use the ABAP Workbench tools, you can follow these steps:
Code the program
We can start coding our program! To make this step easier, I have created a public repository in github to copy + paste the source code. You can enter to the following URL to obtain it:
https://github.com/estewil/abap_address_validation
This program is documented and provides all the steps to perform the consumption of the API. You can simply debug it and analyze each sentence. Feel free to modify it for all your needs.
Keep in mind that you must replace the API_KEY with the one that was obtained in the previous step.
To summarize, the program does the following:
Step 1: Select Customers from Table
The first step is to select the customers that you want to validate their addresses from a table in SAP. For this example, we will use the table KNA1 (General Data in Customer Master), which contains general information about customers such as name, address, country, etc.
Step 2: Concatenate Address Components
The next step is to concatenate the address components of each customer into a single structure that can be used as an input for the Address Validation API. The address components are stored in different fields in the table KNA1, such as STRAS (Street/House number), PSTLZ (Postal Code), ORT01 (City), LAND1 (Country Key), etc.
We will store the customer information in a structure.
Step 3: Convert the structure to a json
In this step we will convert the structure to a JSON, that will be used for sending it in the request. To convert this structure, we will call the method “serialize” of the class:
And the result that should be obtained is like the below:
{
"address": {
"regionCode": "US",
"locality": "Mountain View",
"administrativeArea": "CA",
"postalCode": "94043",
"addressLines": ["1600 Amphitheatre Pkwy"]
},
For more information about Address Validation API parameters, you can refer to https://developers.google.com/maps/documentation/address-validation/requests-validate-address?hl=en
Step 4: Call Google API
The fourth step is to call Address Validation API using our URL string and get the response back from the service. To do this, we will use an ABAP class called CL_HTTP_CLIENT that allows us to create and execute HTTP requests. We will pass our URL string as an input parameter to this statement. We should also complete some header information and execute the method “set_cdata” to “paste” the json to the request.
The URL to be consumed is the following:
https://addressvalidation.googleapis.com/v1:validateAddress? ?key=YOUR_API_KEY
We will then execute our HTTP request using a method called SEND of this class that allows us to send data over HTTP protocol. The method called “get_status” of this class allows us to handle exceptions. It’s recommended to check if there were any errors during this process.
We will store the response data in a string called VL_CONTENT of this class that holds an object of another class called CL_HTTP_RESPONSE that represents an HTTP response message.
Step 5: Check Response
The json that was received must be interpreted to decide to update or not the address. The following link explains in detail the response.
https://developers.google.com/maps/documentation/address-validation/understand-response
Conclusion
In this article, we have shown you how to validate customer addresses from your master data in SAP using the Google Address Validation API. We have explained the benefits and limitations of using this API for address validation. We have also provided a step-by-step guide for creating a report in ABAP that will call the API and display the results.
Some useful links:
The definition of the API can be found
here.
How the result should be interpreted can be found
here.