Address Validation Example
Power Automate is not just a great tool to enhance business logic (like the famous Approval workflows used by so many organizations). It can also add great functionality to PowerApps applications. In this article, I want to provide an example.
I recently worked on a project that required that users can get entered mailing addresses validated. Online shops often use this functionality to ensure, the entered address is correct - prior to accepting an online order. This article is about how to implement this functionality by using a PowerApps application and a Power Automate flow.
The user interface of the PowerApps application wasn't too complicated. The following screenshot provides an example:
The user types in an address into the textbox on top. Once the user clicks on Validate Address, the PowerApp application calls a Power Automate flow, which does all the heavy lifting.
As mentioned, the flow is called once the user clicks on Validate Address. Because the flow is called from within PowerApps, the parameter (the address to be validated) needs to be requested from PowerApps and stored in a variable.
Next, the validation API is called. We decided to use the web service offered by PostGrid, but Canada Post and others are offering similar services too. Calling the web service isn't hard if you follow the guidelines of the provider. In my example, the call looks like this:
I blurred the API key in the above image. Most (if not all) services require to use of an API key which you'll get once you subscribed to the service. If you check the Body section in the above screenshot, you'll notice two things:
As the response is JSON, I used the Parse JSON action in Power Automate to extract the validated address from the response. The Parse JSON action takes its input from the previous HTTP action. To configure this action, just take the output of the previous action (basically the JSON response) from a successful run and use this to generate the schema.
The validated address (or addresses) are copied to an array first, which is converted to a string prior to returning the addresses back to the PowerApps application.
The application takes the response from the flow and lists the validated addresses as items in a Listbox. The user can pick one of the validated addresses or continue with the address entered manually. Well - on theory :-) Unfortunately, there is a limitation in Power Automate as it can only return scalar data types, but it can't return an array of addresses.
This is unfortunate, as it requires some thinking on how to overcome this limitation. My current approach is to create a JSON object with validated addresses, convert it into a string, return it to PowerApps and try to extract the validated addresses. This will definitely require some coding - even in a low-code environment. At the moment, I am fine with returning just a single address, but once I figured out how to return multiple addresses, I will create a follow-up post to this one.
Anyways - I think this example illustrates how citizen developers can take advantage of PowerApps and Power Automate to create solutions, which previously required custom development and a skilled C# developer. Without hardly any coding (except for the few lines in the HTTP action's body element, which could be copied from the provider's documentation (and the tricky return of multiple addresses), I was able to create a solution, that users can utilize to validate mailing addresses.
Update: As mentioned, I was working on a workaround to return multiple addresses. First, I created a web service, that my Power Automate flow calls instead of directly calling the public web service. This allows me to better handle the response from the public web service. My custom web service takes the response from the public web service and creates a long string containing all the addresses. The format I used looked like this: {FullAddress: 1234 Wall Street, Halifax, Nova Scotia, E4W 6D2, Canada}. As it is unlikely, that addresses contain this character (|), O used it to separate multiple addresses. Here is what that looks like: {...}|{...}|{...}
The next problem I needed to solve was how to extract all the addresses from that big string. PowerApps offers the Split() function and it can be used like this:
This call creates a collection (addresses) that contains separated address strings. The following image shows the collection:
Not bad, but the address strings need a little bit more formatting before they can be added to a Listbox. True, I could have used my custom web service to do that kind of formatting, but I was curious, about how to do that in PowerApps. Here is my solution to this:
I assigned the collection "addresses" to the items property of my Listbox hoping the address strings would show up. They did :-)
Lesson learned: there are ways to work around some limitations of Power Platform, although they sometimes require some thinking outside of the box. My solution might not be the most sophisticated one, but it works for me and -that's important- I learned something new regaring handling collections in PowerApps!