Dynamics 365 for Operations and Logic Apps – How to use a parameter as the connection URL
Logic Apps now come with a Dynamics 365 for Operations Api (currently in preview). This allows you to configure flows that use data from your Dynamics 365 for Operations instance.
Multiple url paths
Let’s say you create a Logic app that finds a Sales Order in Dynamics 365 based on some criteria. If the Sales Order is not found, it will create the Sales Order Header. After this it will proceed to create a Sales Order line. As per the following diagram:
In this case you are calling the Dynamics 365 for Operations API three times. Although this only creates on API Connection, when browsing through the logic app in code view, you will notice that each instance of the API is hardcoded with the url of your Dynamics 365 connection ex:
"path": "/datasets/@{encodeURIComponent(encodeURIComponent('yourd365url.cloudax.dynamics.com'))}/tables/@{encodeURIComponent(encodeURIComponent('SalesOrderHeaders'))}/items",
There are a few reasons why you might want to make this url a parameter ex:
a) To make the code more maintainable ex. to aid deployment in another environment, you might want to set the url only once.
b) To make the url a variable that can be defined at runtime.
In this example I will show how to resolve point a) by using parameters. This should also give you a clear picture of how to set the url at runtime.
Parametrizing the url
This can be done as follows (assuming you are using Visual Studio for your development).
1) In the initial parameter declaration, declare a parameter for your connection url ex:
"dynamicsax_api_Connection_Url": {
"type": "string",
"defaultValue": "yourd365url.cloudax.dynamics.com"
}
2) In the parameter section of the logic app definition, reference the connection url as follows:
"dynamicsAx_connection_Url": { "type": "string", "defaultValue": "[parameters('dynamicsax_api_Connection_Url')]" }
When this code is deployed, it will replace the defaultValue of dynamicsAx_connection_Url with the value of dynamicsax_api_Connection_Url
3) In the methods which call the api, you can now reference the url using the parameter instead ex.
"path": "/datasets/@{encodeURIComponent(encodeURIComponent(parameters('dynamicsAx_connection_Url')))}/tables/@{encodeURIComponent(encodeURIComponent('SalesOrderHeaders'))}/items"
Please note: Once you perform this change, the connection between the logic app and D365 will be broken at design time so you will not be able to perform certain operations using the UI. For instance, you will not be able to view the fields to set when performing a create call.
*This post is locked for comments