Thursday 10 January 2019

How to identify a Contacts' time zone in Flow using CDS and Bing Maps

23:11 Posted by Benitez Here , , , , , No comments
Have you ever needed to find the time zone of a Contact? This WTF episode is for you then my friend. Time zone can be easily identified through  the power of a Bing Maps API using a couple of connectors and an expression in Flow. No code!

Bing Maps Time Zone APIs

Before I share the steps with you, I want to talk briefly about the Bing Maps Time Zone APIs available as I'm using one of them in my Flow.

Last year the product team made Bing Maps Time Zone APIs available which you can read from this Bing Maps blog post.

The Bing Maps Time Zone API I am using is "Given location coordinates, find the time zone of the place."


I decided to use this one instead of "Given a place name, find the time zone of the place" because personally I find that using latitude and longitude coordinates would be more accurate rather than using a place, such as a city.

To use the Bing Maps Time Zone API (and Bing Maps Flow connector which I'll get to soon), you need a Bing Maps key. 

I signed up for a Bing Maps Basic Key to use a Bing Maps Flow connector and the Bings Maps Time Zone API. If you are going to be using this in your organisation or if you are a consultant and will include this in your solution design, you need to review the Bing Maps licensing options beforehand to understand if this is a valid solution design.

Steps

1. Trigger

For the trigger of my Flow, I'm using the When record is created CDS trigger. I want the Flow to trigger when a Contact is created.

2. Bing Maps action

The next step in my Flow is to use the Bing Maps action of "Get location by address." This will retrieve latitude and longitude coordinates from address data. We will use the address data from the Contact. The assumption here is that an address is entered against the Contact in the model-driven app in the Contact entity.


When the Bing Maps action successfully returns an output, it looks like the following where you will see the latitude and longitude coordinates.

3. HTTP action

To use the "Given location coordinates, find the time zone of the place" Bing Maps API we need to use the HTTP action and reference the URL provided in the Bing Maps blog post.

https://dev.virtualearth.net/REST/v1/TimeZone/latitude, longitude?key=<bingmaps-key>

Simply reference the latitude and longitude outputs from the previous step, followed by inserting your Bing Maps key.

Result

The Bing Maps API will then successfully retrieve the Time Zone of the Contact. Pretty cool right?

One step further - saving the time zone value

If you need to sore the time zone value of the Contact, you can do so by creating a custom single line of text (string) field in your Contact entity in CDS. The property that will be retrieved from the JSON response is the windowsTimeZoneId from the timeZone object. This is what will be stored in the custom field in CDS.

There's two options that will allow you to retrieve the windowsTimeZoneId property successfully.

Option 1 - without using an expression

You can use the Parse JSON action where you'll be required to copy and paste the JSON response  from the HTTP action to generate the schema.


The next step is to use a Compose action. When you select the Compose action and review the Dynamic Content values presented, you won't see the timeZone object as it is in the array of resources as seen in the JSON response.


If you select resources, you'll then see an apply to each magically appear. John Liu has a blog post that outlines what you need to be aware of when using the Parse JSON action and he briefly talks about this behaviour. When you add another Compose action, the windowsTimeZoneId property will now be visible.


It's not a bad option but it may be confusing.

Option 2 - with an expression

John Liu explained to me that if you don't want to use the Parse JSON action and simplify your Flow, you can use a Compose action to retrieve the windowsTimeZoneId property.


The expression to use is
body('1.2_HTTP').resourceSets[0].resources[0].timeZone.windowsTimeZoneId

My way of explaining this expression is to work backwards:
From the HTTP action, get the windowsTimeZoneId property from the timeZone object which is in the resource array whose parent object is resourceSets.

If you have a better way of explaining this to a non-technical soul, you can tweet to me 😁

The final step

For either Option, make sure you have an action that will update the custom field in the Contact entity.

Summary

By using a Bing Maps Flow action, a Bing Maps Time Zone API and a Compose Flow action you can retrieve the time zone of a Contact in CDS through the address information without code. So cool! Flow is the best.

This WTF Flow is available for download on the TDG Power Platform Bank.

#WTF #FlowFever #FlowAllDayEveryday

Help me get to 500 YouTube subscribers

As mentioned in my vlog, subscribe to my YouTube channel if you haven't already done so as Will from TDG will supporting my milestone achievement by doing a sheperd's pie vlog. This was mentioned in the Power Apps podcast episode where I was a guest and chatted to Shawn, Chris and Will. It would be great to see in Will in the kitchen 😄

Jokes aside, it would be great if I reach 500+ subscribers. I started in 2014 and I'm going to keep going till I can. Thank you!

0 comments:

Post a Comment