While writing my previous post regarding Flow as a scheduling mechanism, I stumbled across this post demonstrating how to execute Web API calls from Flow.
Why would you want to execute Web API calls from Flow/Logic Apps?
While Microsoft Flow/Logic Apps support basic Dynamics 365 operations, there are many missing functions that can simplify common integration scenarios between these platforms: executing Custom Actions, triggering Processes, handling metadata and issuing complex queries.
Some of these problems can be worked around using the Command Pattern, but using the Web API from within Flow/Logic Apps provides a simple and powerful integration mechanism.
In this post, I’ll demonstrate how to authenticate and execute Web API functions and actions from Flow (a similar process can be used with Logic Apps):
- Execute a FetchXML query
- Execute a Custom Action
The requirement used to demonstrate is again the automated weekly evaluation of Leads: once a week, any Lead which is older than 5 days and not rated hot is disqualified.
With this implementation approach, Flow is used for scheduling, query for target business records and applying a Dynamics 365 Custom Action to each business record.
- Have access to Microsoft Dynamics 365 online instance and Flow environment
- Register Microsoft Dynamics 365 online instance in Azure AD and have the Application Id key ready.
Make sure you set the oauth2AllowImplicitFlow as described here.
- Download and import Solutions
Download and import this Flow solution into your Flow workspace.
Download, import and publish this unmanaged solution into your Dynamics 365 instance. It contains one Custom Action that will be executed from Flow using Web API.
- Set Flow Settings
Edit the newly imported Flow
Set the following keys with values to match your environment
- Note the following settings
This variable holds FetchXML query to retrieve the target Lead records.
This variable holds the target entity name as it is used with Web API
This variable holds the target Custom Action to handle each Lead.
Note that for unbound Custom Action, the Microsoft.Dynamics.CRM should be removed.
This action requests a token used to authenticate to Web API
This action executes the FetchXML query
This action executes the target Custom Action for each Lead record returned by the query
- Test your Flow
After a successful run, Flow will display the number of affected records.
In Dynamics 365, these records should appear as disabled now.
If your Flow fails on the RequestOAuth2Token action, try this solution
- I prefer authenticating using an Application Id and Secret via an App user rather than plain user credentials.