top of page

Use Power Automate for currency conversions


I blogged recently about embedding canvas apps into Model-driven apps and how this can now be easily done. As part of my Scottish Summit presentation I embedded a canvas app into a Model-driven app and then took the prices of products in D365/CDS on the form, and converted them into a target currency. This is just one of the many things you can but, but the Power here comes from Power Automate.

Power Automate has the ability to connect to API’s, meaning it can be used to push and pull information from different sources. In my example below, I am triggering the Flow by pressing a button on a canvas app, but this could be an on-demand Flow or triggered from a webhook using a customer button in D365/CDS .

Create a new Flow and choose your type and trigger, in this instance we want a Instant from Blank and choose Power Apps as the trigger. This trigger requires no more configuration as you will call it from your canvas app.

The next step is calling an API and for this we will use the HTTP action in Power Automate. There are free API’s for currency conversion as well as paid ones, depending on the one you choose determines the options you have when using them. In my example, i use fixer.io which has a free version and paid version. Sign up to the website and you will get your own API key to make requests.

We want to use a GET request so that that to the method and then in the URI paste the details below with your API key, alternatively, this string is available when you sign up for a fixer.io account.

http://data.fixer.io/api/latest?access_key=<access_key>

Next, we need to take the request and parse the JSON so that we can use this in the canvas app. To do this, we need to know the schema and write the JSON for that...but that’s a lot of work and I’m not a developer. Instead, Power Automate will generate the schema for you based on a sample. The easiest way to do this is to run the Flow and the output of the HTTP action will give you the JSON schema you require.

Create a new step, choose Parse JSON and then Generate from sample. Paste in the output from the HTTP action and then click done.

The last part of the Flow is just a Request Response to say it completed successfully.

Your finished Flow should look like this.

Next, we need to go to the canvas app and trigger the Flow. Create a button and on select we can paste the below text into the expression editor:

'PowerApps-GetExchangeRate'.Run()

The text inside the ‘’ is the name of the Flow we want to run. Putting this on select of a button will run the Flow, but we want to handle the data coming back, so to do this we want to make a collection so we can handle this data. To do this, in the select expression we want to create the collection with the following code:

ClearCollect(CurrencyRatesForPA,'PowerApps-GetExchangeRate'.Run())

This allows us to use the CurrencyRatesForPA collection to filter and get the exchange rates. To do this, you can pass in a currency code and return the first value from it, since the table/column only has one value per exchange rate.

First(CurrencyRatesForPA).rates.GBP

But we want this to be dynamic, so in my example, I created an optionset with a list of Currencies/Currencies codes and then used this to return the exchange rate.

Switch(

DrpCurrencySel.SelectedText.Value,

"GBP", First(CurrencyRatesForPA).rates.GBP,

"USD", First(CurrencyRatesForPA).rates.USD,

"CAD", First(CurrencyRatesForPA).rates.CAD,

"JPY", First(CurrencyRatesForPA).rates.JPY,

"EUR", First(CurrencyRatesForPA).rates.EUR

)

The DrpCurrencySel is the name of the option set, then the switch control will determine what currency we are passing in. This formula runs on a box where the exchange rate will be displayed. If we want to convert a price/cost into that currency, we need to multiply this by the exchange rate. In a new box we put the following formula:

If(!IsBlank(LabExchangeRate), ModelDrivenFormIntegration.Item.'Price Per Unit' * Value(LabExchangeRate.Text),"")

Because i’m using a canvas app embedded into a Model-driven app the ModelDrivenFormInterration.item.’Price Per Unit’ gets the price of a product, but if you have the price/number in another field, you can just link that to this.

You can then write this data to D365/CDS or do whatever you want, but the Power comes from Power Automates ability to interact with API’s, which unlocks endless possibilities.

Ciao for now!

bottom of page