How to extend your PowerApps model-driven / Dynamics 365 integration with Bing Maps

How to extend your PowerApps model-driven / Dynamics 365 integration with Bing Maps

If you are wondering how you can embed Bing Maps to a custom / another form besides account, contact, lead etc., enabled as standard, you need to read this. You will find out a way of displaying your records on the map by generating valid latitude & longitude pairs for the address the users enters.

This article provides only a personal recommendation of doing this by using Microsoft Flow, Bing Maps and PowerApps Model-Driven Apps.

The following will explain how to achieve this.

You are starting by creating a custom HTML web resource in order to display Bing Maps on your form.

In my example, I'm embedding the Bing Maps web resource to the Reservations form (I'm using the sample model-drive app "Asset Checkout" ).

No alt text provided for this image


If you are a developer but have never done this, you should know that there are many resources and sample code examples, which might help you creating this web resource. For Dynamics 9.0+ I highly recommend using the Client API object model (execution context) instead of Xrm.Page to build your code.

As a tip, to create the Bing Maps HTML web resource, I was guided by following documentation articles:

Creating a Bing Maps Control

Bing Maps in CRM (samples)

For building this web resource, you have to retrieve latitude and longitude values to pin the record on the map (location of the address). In my case I took those from the account record (associated with the reservation via 1:N relationship - lookup field). Understanding how getAttribute and RegardingObjectID work, will definitely be helpful.

Note: Don't forget that you need to register a Bing Maps Key (for development/test purposes, it's free to use).

After you created the Bing Maps custom web resource, then as a next step, you would like to automatically generate latitude/longitude pairs for the user-inputted address line.

For this, as a next step, I will show you hot to geocode your custom entity record by the means of the Bing Maps Microsoft Flow connector.

The current position of the record is positioned on the map (pin) based on the latitude and longitude fields. Having users enter the coordinates manually is not a user-friendly option, so, the easiest way in my opinion is by using the Bing Maps - Microsoft Flow connector to automatically generate them.

Other options for developers would be LogicApps or a Dynamics 365 Plugin that connects to Bing Maps API.

Back to this example, by the means of Microsoft Flow, the Bing Maps connector can generate valid latitude & longitude coordinates based on address information (street address, city, state, country, zip code) stored i.e. in the account record.

Depending of your preferences and also licensing plan, you can use CDS for Apps or the Dynamics 365 Microsoft Flow connector to achieve this. In this example I tested both options who work perfectly. There is an important trick that you have to use with CDS for Apps, which I will detail when covering this alternative.


Option 1 - Using Dynamics 365 Microsoft Flow Connector

For the sake of this example, I'm using the "When a record is updated" trigger, because for testing purposes I wanted to trigger the Flow multiple times when changing the account (lookup field) associated with my reservation.

In a real example, maybe you would like to use another trigger or even a HTTP POST Request, that will limit the flow runs only when the account lookup field gets updated.

In the Flow logic, as the first step, becuase I'm using the account associated with my reservation to hold address information, I have the "Get record" action (if you store address information directly on your custom entity, this step would not be required).

Following (and also the most important step), I added the Bing Maps - "Get location by address" action that will output valid latitude and longitude coordinates based on information stored in the account record (added as dynamic values).

No alt text provided for this image

As the last step, I'm updating the Reservation record with the latitude and longitude values generated by Bing Maps and and also add address line values (which are required only if you would like to display the fields on the form).

I also added city & country information by using dynamic values from the Get Record - Account step, but these values can also be retrieved from the previous "Get Location by address" step (important for displaying the record on Bing Maps, is to generate latitude and longitude values)

No alt text provided for this image


Check below the outputted result of the Bing Maps - Get location by address step:

No alt text provided for this image


Option 2 - Using Dynamics 365 Common Data Serive for Apps Connector

If you are creating model-driven PowerApps and do not use Dynamics 365 or have Dynamics 365 but prefer to use the CDS for Apps premium connector, then you will be interested to see the trick that makes the Flow work :)

In my example, I used now the "When a record is selected" (the user runs the flow from the model-driven command bar). Although, this is only an example, you can use another trigger to initialize the flow.

Next, you have to get the values from both reservation and account records (will use them later on for dynamic values)

Note: I observed that if I'm not getting the account record and am using the Dynamic Value "Account" from the trigger "When a record is selected", the GUID of the account associated with my reservation is not identified correctly (odd enough, I didn't investigate this...so instead added the Get record action).

No alt text provided for this image

Next action, the Parse JSON step (trick that will save you hours of testing different crazy ideas of generating valid string values and not getting null...)

I struggled a little bit understanding why dynamic values cannot be passed in a similar manner as before to the Bing Maps connector, but it seams that the data type is not recognized as strings. Hereby, you have to use an intermediary step "Parse JSON" to read the Account record address information and afterwards use the Parse JSON Output for passing values to the Bing maps connector.

No alt text provided for this image

Note: If you never used Parse JSON, you can copy the JSON schema from the previous step (Get record - Account) and click the "Use sample payload to generate schema" button, paste the schema/output there and a JSON schema is generated for you.

You will receive "The schema validation failed" error due to some fields who throw invalid type errors (number instead of int, null instead of string etc.). You can easily remove them from the JSON Schema. Anyway, the preferred option would be to keep only those properties needed for the next step (address line, city, country etc.)

No alt text provided for this image

Next, use Bing Maps to generate latitude and longitude values and afterwards, as the last step, update the reservation with address information to have the record displayed on Bing Maps.

No alt text provided for this image

Now, presenting the working results :)

Run example

Before running the Flow, all address information fields are empty for my reservation (the map is not displayed).

No alt text provided for this image

By adding an Account in the Lookup Field and manually triggering from the command bar the flow (of course, like I mentioned, this can be automated), the Flow will run and the record will be displayed on the map.

No alt text provided for this image

So, basically, long story short, these are two ways of running such a Flow, depending on what connector you can and want to use.

P.S. This is my personal opinion and represents a recommendation that is not documented in an official source. All the logic presented in this example should be used as guidance only.

Happy holidays!

Cheers,

Alex


To view or add a comment, sign in

More articles by Alex Dan

Others also viewed

Explore content categories