Integrating Dynamic 365 CRM with Third party API via custom connector using Power Automate – Part2
This article is in continuation of Part1 where I tried to give you a comprehensive description of Creating Custom Connectors in Power Automate for third party system API which enables a clearer understanding of custom connectors and its functionality. In Part2, I will give you a brief idea about how to integrate Dynamic 365 CRM with a third-party API via custom connector using Power Automate with a real time business use case.
I will take few moments to explain about multi-currency handling in Dynamic 365 CE.
The base currency is set while setting up the instance or the environment. There can only be one Base currency for instance or environment. Most importantly, the base currency cannot be updated.
But you are selling your products in the global market. It is important to understand that you will have prospects using multi-currencies. Dynamic 365 CE OOB supports you to create multi-currencies seamlessly. The below mentioned screenshot illustrates the default currency record in Dynamic 365 CE which consists of currency code, currency symbol, currency name and exchange rate.
To define additional currency, navigate to setting -> Business Management
We can create additional currencies either system or custom. To create system currency, we can select any of the existing currency codes.
For instance, while we create an opportunity record, Dynamic 365 CE automatically sets the default currency for the user.
Based on the customer, the Rupee currency is set in opportunity record.
While calculating the base amount, Dynamic 365 CE considers the Exchange Rate defined in the currency record.
Unfortunately, Dynamic 365 CE does not update Exchange Rates automatically. We should have some mechanism to update the exchange rate on a daily basis.
Let’s Start!!
Our primary goal is to capture live Exchange Rate from third party API and update the currencies records in the Dynamic CE on a daily basis. So, it is better to create a scheduled cloud flow which runs every day.
Step#1: Create a Recurring flow
In the next step, we must provide the trigger timing details. Define the starting date and time and repeat frequency. As per our requirement, we want to run it daily so choose to repeat every 1 day at 1:00 PM.
Step#2: Initialize Variable
Now we will add an action to initialize the variable to set the base currency code.
Step#3: Retrieve Currencies detail from Dynamic 365 CE
Add a new action to retrieve Currencies records from Dynamic 365 CE by using Dataverse connector and action as List rows.
Select columns:
Enter a comma-separated list of columns to return. In our case, selected few columns like “transactioncurrencyid,currencyname,isocurrencycode,exchangerate” from currencies table.
Filer Rows:
Filter expression to narrow down the set of rows that Dataverse returns such as “statecode eq 0” for rows with state code is equal Active.
To exclude base currency, the added condition to check “isocurrencycode ne USD” for rows with currency code is not equal to USD.
Step#4: Create Currency Code Array
Add Data Operation - Select action to create a new array from a list that contains currency code.
Note: Although you can add or remove elements by using the select action, you can’t change the number of objects in the array.
Step#5:
Time has come to use our custom connector to get a live exchange rate for a given currency code list. Select Currency Exchange Rate action from choose an operation template.
As we know, the currency exchange rate takes two parameters.
1. Source – Base currency is passed.
2. Currencies - Specify a comma-separated list of currency codes. So, I used join expression to join array values with separators of comma delimiter (,).
Expression:
join(body('Select_Currency_Code'), ',' )
In the background connector calls the API, and you get the response, which includes the live Exchange rate for a given currencies list.
My initial thought is to create a cloud flow which runs on a daily basis to capture live Exchange Rate against currencies and update in the Dynamic 365 CE. But my thought processes evolve to add more capabilities to match with real time business process flow. Updating Currencies Exchange Rate is not quite that simple. It will go through several levels of approval process before the exchange rate is reflected in the Dynamic 365 CE. So, I decided to incorporate a PDF file generation and approval processes functionalities.
Step#6:
Using Select - Data Operation action to create a new array which contains currency code, currency name, latest exchange rate and GUID. This array will help us to easily format data, generate a PDF file and update exchange rate values in Dynamic 365 CE.
To get currency exchange rate from API response JSON object. I have written an expression to parse the response object using the output function by passing currency code to pick the exact exchange rate value from the API response JSON object.
Expression:
outputs('Currency_Exchange_Rate')?['body/quotes']?[concat('USD',item()?['isocurrencycode'])]
Step#7:
I want to present the data in HTML table format. The first and foremost option is to select Create HTML table - Data Operation action. This action will change a JSON array input into an HTML table seamlessly.
Columns are mapped either automatically or header and values are mapped manually. In our case, we have mapped only essential columns to create an HTML table.
Step#8:
Create a new step using compose operation action and enter the required CSS style for the table to generate a PDF file.
Step#9:
I picked a OneDrive connector to create a file (html file) and convert file (PDF file) seamlessly. As a first step we will use Create file action to create an HTML file in OneDrive.
Step#10:
Add convert file action to convert a HTML file into PDF file format using the path.
supported file conversion formats are html, htm, docx, msg, ppt, pptx, xls, xlsm, xlsx etc.
while using convert file action, we should be aware of known limitations and file size constraints in place for the protection of the connector's service. note that attempting to perform operations beyond the limits will result in rejection, errors or timeouts. Few key important limitations are listed below.
Step#11:
A PDF file created successfully. Now time to attach the PDF file and send it for approval.
Create a step using "Start and wait for an approval" action and select "Approve/Reject-First to respond" for approval type.
Approval type: when a flow with Create an Approval action is configured with Approve/Reject - First to respond, it waits until it's assigned to approve or assigned to reject the approval request.
Step#12:
To evaluate the user response is equal to Approve! then we need to add a "condition" action to handle the response from the previous approval action.
on the condition card, select an empty area in box on the left. The dynamic content list opens. Select the Outcome parameter to add it to the box. In the box in the middle of the condition card, select is equal to. In the box on the right, enter the "Approve" text which is mentioned in the approval type.
Step#13:
If yes block:
If Outcome is equal to "Approve" then add Apply to each action to loop through each item from live currency exchange rate list and update Dynamic 365 CRM currencies records with latest Exchange rate.
Step#14:
Finally, I decided to store the exchange rate PDF files in SharePoint for future reference.
Using SharePoint connector, add create file action to enter file name and output of converted PDF file content body is passed to file content for the create file action. Select correct SharePoint site address and folder path where we want to store the file.
All set! Let's see the cloud flow in action.
The Old Exchange Rate cited below
Live Exchange Rate is created as a HTML file in OneDrive
HTML file is successfully converted to PDF and attached in approval email.
When approver approves the Exchange rate then Cloud flow will update the latest Exchange Rate in Dynamic 365 CRM. The Below screenshot is the updated Exchange rate.
Finally, Cloud flow will store the approved versions of the Exchange Rate PDF file into SharePoint for future reference.
`Perhaps this article will also give you some inspiration on other ways to make use of Power Automate.
Happy Learning and Happy Sharing. Cheers!!