Building a better sync between Salesforce and MailChimp

MailChimp is great. Salesforce is great. And it's not that MailChimp's tools for syncing to Salesforce's data isn't great, it's just not what my clients need. So the goal is to build a simple a sync as is possible, and keep a very clear seperation between areas of concern. MailChimp will worry about sending emails and keeping track of those analytics, Salesforce will be the 'repository of truth' in terms of the CRM data. And that's it.

This code package is fairly large, and is being deployed into a few of my client's instances as I write this. If it's something that you're interested in chatting about, please let me know.

OAuth2 or Bust

Username + password + token = nope. We use an OAuth2 connection between the systems, with instructions about how to set this up as part of the install. If you're not sure about the what/where/how for OAuth2, you can check here. One important note is that while the Sync application is Aura/Lightning, this connection mechanism requires a VisualForce page to call back to. Taht said, this page doesn't need to exposed via a public site, since you are already logged in to Salesfoce! After connection, the process will let you know which MailChimp API endpoint that you'll have to open up in Salesforce.


OAuth2 Connection


Once we're 'locked' to MailChimp, we then need to 'lock' to one list/audience. One list, and one list only. All Salesforce Contacts and Leads will up in thist list.


Main List

The Mappings

Pretty standard for this type of application, we need to able to 'map' our Salesforce fields to the proper fields in MailChimp. You can create the fields in MailChimp, or create them in Salesforce, no matter.


The Jobs

Data isn't synced instanteously between the two systems, and it shouldn't be. This issue isn't in changing a piece of data in Salesforce, and needing to tell MailChimp. It's when you change one piece of data on 50,000 records, and then try to talk to MailChimp 50,000 times really quickly. We have to 'bulkify' our calls.

This app looks at two types of data: the CRM data (Contacts and Leads), and the data about which campagins these folks are in. Make a change to either data type, and it gets 'Flagged' for an update when the next job runs. Since there are two types of data, there are two jobs, and they can run anywhere from once a day to once an hour. And just in case you really do need to update right away, you can force the sync jobs to run right away.

The Really Good Part

The unfortunate part of MailChimp's syncing tool for Salesforce, is that when a contact unsubscribes or a messages bounces, that critical information stays with MailChimp only. That's not what we want, because Salesforce is supposed to our 'repository of truth', not MailChimp. We need a way to allow MailChimp to talk to Salesforce, to tell us right away when those unsubs or bounced messages happen.

Step One: A RESTful endpoint is opened up in Salesforce under Sites that MailChimp can send a webhook into. It is open to the world, but it is also very much looking for an API Key to be sent in the request. The only downpoint is that this isn't a mechanism that can be created easily via Apex, even using metadata API calls. This part of the setup is manual, but it is worth it!
Step Two: We need to create a webhook call in MailChimp that sends Salesforce data when things like unsubs happen, and it needs to do it via an API Key. Because MailChimp's API allows for webhook creation, the only thing left to do get this part working is the click of a button.
Source Code

This is several thousands of lines of code, and I'm not sure where this application is going, or what it is going to become. I'm not releasing the Source for this yet until some of those questions get figured out. If it something that you are interested in, or if your organziation is looking for an implementation similar to this, feel free to reach and let me know.

Corey Broderick
info@coreybroderick.com

Phone

+1 (647) 367-1601

+502 5985-5412

Address

Port Hope, Ontario, Canada
Gutemala City, Guatemala

Email

info@coreybroderick.com