Shopify, MailChimp, and D365 with Power Automate
Some time ago, eCommerce solution-provider Shopify, and direct-mail management site MailChimp had decided to part ways. This presented a particular problem as I was using that relationship to sync Shopify customers with Dynamics 365 through MailChimp since they automated a lot of the GDPR and CCPA requirements and I could just record the data in my CRM. Thankfully, Microsoft Power Automate steps in with some easy-to-use automation tools to help fill that gap.
HTTP Request Trigger
This particular scenario required adding new Shopify customers to D365. If they opted into marketing communications, they would also be added to MailChimp and the corresponding flag in D365CE ("Do Not Allow Bulk eMails") would be set accordingly. The first issue, however, is that there isn't a Shopify connector in Power Automate. Not long ago, I would have used a workaround, but now I understand a bit more and am able to capitalize on the friendly use of webhooks in both Shopify and Power Automate.
We will begin in Power Automate and create a new flow with a trigger, "When an HTTP request is received" and add a compose action which we'll name "Release Notes" so that our flow has a trigger and an action, and can be saved.
One trick I learned in the Microsoft Technology Expo was to add a Compose action simply to track notes about your flow. I'm doing this pretty regularly now, but this also lets us save in order to generate the POST URL which we will need for the next step in Shopify.
Webhooks in Shopify
To connect Shopify to Power Automate, we'll go into the Shopify Admin center and set up a webhook. We'll do this by clicking on Settings, then Notifications, and then scroll to the bottom of that page.
At the bottom of the Notifications screen is a section called "Webhooks" where we will build the connection to Power Automate. Click the "Create Webhook" button, and find the event trigger you'd like to use for your flow.
For my example, I want to select "Customer creation" so that my flow is triggered with each new customer. The format is JSON, and then the URL will be the link that is generated in your trigger once you saved. Click "Save webhook" to finish.
Once that is saved, you'll see your webhook in the list and can scroll to the far right of that list to click on "Send test notification". This will send sample data to your flow, which we can use to build the schema for the trigger.
Connecting Power Automate to Shopify
Head back to your flow and look at the most recent run. Once there, open up the trigger to copy the contents of the body to your clipboard.
Click on "Edit" in the top right so that you can make changes to your flow, and then open up the trigger at the top. Below the empty box labeled "Request Body JSON Schema", there is a link to "Use sample payload to generate schema". Click on that and paste the contents of your clipboard into the window that pops up. Click "Done" and you should be returned to your trigger, but now there will be an automatically generated schema in the window. Take a look at John Liu's relevant article, A Thesis on the Parse JSON action in Microsoft Flow, and consider Problem 4. Even though this isn't the Parse JSON action, it will behave much in the same way if the data doesn't line up with what Power Automate is expecting.
Adding the Lead to Dynamics 365 CE
The next action will be to Create a new record in the Common Data Service. Select your environment, and "Leads" for the entity, which will open up a host of other fields to fill in.
Note that everything with a red asterisk at the top is required by D365CE. This presents a small problem as Last Name is not a required field in Shopify, so our flow has an opportunity to error out if we don't plan ahead.
For my example, I used the coalesce() function, which will return the first non-null value in a series. I use this particularly when cycling through phone numbers in a contact, but here we can add a default value to the end like this:
This will add the words "No Data" in the Last Name field if Shopify does not have that information to pass along. Another option would be to use an if() in combination with empty():
Both will work, but I think the coalesce is a bit more elegant...and probably faster.
Adding the Lead to MailChimp (If Appropriate)
One of the bits of data coming from Shopify is whether or not the customer opted into marketing materials.
We'll add a condition to check the "accepts_marketing" value from the trigger. If true, we can add it to our MailChimp list to send promotional eMails. If that value is false, we won't add it to the MailChimp list, and will also want to mark the record in D365 to disallow bulk eMails.
Inside the condition's "Yes" path, add the MailChimp action Add member to list, and select the appropriate list, status, and eMail address. My suggestion here is to use the name and email address from the trigger rather than the D365 record to avoid sending a direct mail campaign to "Mr. or Ms. No Data".
Going down the "No" path of the condition, we'll simply add a CDS action Update a Record to update the Do Not Allow Bulk eMails field to "Yes" which translates to "true" in D365.
Bonus Material: Tag a MailChimp Subscriber Using Power Automate
One thing that I found disappointing with the standard MailChimp connector is that we don't have the ability to add a tag to a subscriber as we are adding them (or after, for that matter). This presents a problem as the most effective way to segment a marketing list is by using the tags assigned to each segment. To that end, I wanted to keep track of the origin of this subscriber so that I knew they came in through the eCommerce site. I can do this with the Source Campaign or Topic in Dynamics 365, but I also want to track that information inside MailChimp. Using their front-end, it's fairly straight forward...but using the API and Power Automate takes an extra couple of steps.
The first step in the process is to get an API Key following these instructions, then store those in a Compose action anywhere in your flow (right up at the top is good). Be sure to Secure the Inputs as shown in Part Two of the Birthday Bot.
Also, in the Birthday Bot series, Part Five covers a good way to get ID's for various things by setting up a quick flow with a manual trigger and an HTTP action.
Use this method to find the IDs for the List/Audience and the Segment/Tag for the user you want to edit. Rather than adding a tag to a user, what we are actually doing is adding the user to the segment (list of subscribers with that tag). Ashley Rogers found this link on that to get you started - Getting Started with Tags.
By using all of that information in an HTTP POST action directly after the Add Subscriber step in the "Yes" path of my Marketing condition, I am able to ensure that all available information is available to my users, even if they aren't using Dynamics 365 to view the information.
If you're just getting started in Microsoft Power Automate and are looking for a quick guided-learning session, find my Micro-Job on Collab365 and we can chat about the things getting in the way of your flow, and I can point you in the right direction.
Get in and make a mess of things. I make so many mistakes building these things with the hope that it will save you some trouble as you're automating your world. It's a great way to learn some things and have fun along the way.