In this post, we’ll take a quick look at how to perform a loop, or “for each” statement, using Microsoft Flow. The scenario will be as follows: An Account gets updated in Dynamics CRM online with a new owner, we need to kick off a process that sends an email to every contact in the account with information regarding the new Account Owner. While we could easily accomplish this using a standard workflow in Dynamics CRM, the scenario lends itself well to test the loop and filter functionality in Flow.
Before getting started with Flow we’ll need a way to tell that the owner has changed when a record is updated. While this is outside of the scope of this post and well documented in various discussion forums, I’ll provide a brief overview of how I accomplish this: I’ve created a field on the Account entity called “Previous Owner” and have updated the field on every Account record to match the value of current owner. The process we create in Flow will match the fields back up once it is done processing.
- First we need to create a new field to track the previous owner
- I’ll login to Flow (https://flow.microsoft.com) and click on “My flows”
I’ll click on the “Create from blank” button to create a new Flow from scratch
I’ll specify a trigger of “Dynamics CRM Online – When a record is created”
I’ll select my Organizations name from the drop down list and choose “Accounts” for my entity name. This tells Flow that I want this process to run whenever an Account is updated in my Organization.
Click on the “New step” button and select “Add a condition”
Next, we’ll need to check if the Owner field was update. In the Condition block select “Owner” for the Object Name “is not equal to” for the Relationship and “Previous Owner” for the Value. Then click “Add an action” in the “If yes” block.
We need to retrieve the contacts for the updated account. Select “Dynamics CRM Online – List Records” as your action. Select your CRM Organization and contacts as your entity name. Click on “Show advanced options” to create a filter for the query to return only contacts of the updated account with email addresses.
- In the textbox labeled “Filter Query” enter “_parentcustomerid_value eq #account# and emailaddress1 ne null” where account is selected from the Outputs of the previous step.
For specific details on how to write these queries, check out the following MSDN article https://msdn.microsoft.com/en-us/library/gg334767.aspx. For details on the Web API Entity Reference (what fields/field types can I expect for each entity when querying with the API) check out the following MSDN article (https://msdn.microsoft.com/en-us/library/mt608066.aspx)
All that is left at this point is to send an email “for each” contact (performing our loop). We’ll click on the “New step” button following the “If yes” block and select “Add an action”
Select “Office 365 Outlook – Send an email” as your action. If you haven’t configured your connection to Office 365 Outlook, you’ll need to do so at this point.
Select “Email” in “Outputs from List records” as your “To” value. Notice that Flow picks up that the email is coming from a list of records and it will automatically create a “for each” container for you.
Enter a “Subject” and “Body” for the email (you can use values from fields in both the updated Account record and the Contacts List record.)
Add a new step to sync the Owner and Previous Owner fields of the Account.
- Give your “Flow” a name and click the “Create Flow” button at the bottom of the screen.
Test your flow by changing the owner of an Account in Dynamics CRM. In the Microsoft Flow portal, you can check the status of your Flow by clicking on the information icon next to it.
You’ll see a list of successful runs and failures (if any) for your Flow.
Clicking on the arrow next to any of the runs, will show you a diagram of the completed flow.
The following diagram is from a failure. Here, the failure is caused after the flow is triggered a second time by syncing the Owner and Previous Owner fields… It results in an Account update; which is the trigger for our flow, followed by a condition failure (the owner and previous owner now match.) In theory, our conditional block should have prevented this error by going down the “if no” path.
While I’m tempted to chalk it up to Flow still being in preview, I’ll admit I’m no expert, at least not yet😉 and will do a little more digging around to see if I can clear this up. In any case, it serves as a good example of what a failure looks like.