Salesforce - Mailchimp Custom Integration

Client

Client is a UK based design and production service company. They are known for creating fonts of extremely high quality. They provide a strong library of fonts and also provide a service centred around good design.

Challenges:

The requirement was to integrate the MailChimp Account of client to their Salesforce instance. Client wanted to Unsubscribe the Contact/Lead records from their MailChimp list when someone checked ‘Email Opt Out’ checkbox on the contact and Lead record and also when someone unchecked ‘Email Opt Out’ checkbox on Contact/Lead records the that record should Re-Subscribe back into MailChimp list.

The ‘MailChimp for Salesforce’ App doesn’t provide above mentioned functionality.


Solution / Approach:

As per our analysis of the given requirement we cames up with following approaches:

  1. Apex Trigger & Helper Class:
    In this approach we have created an Apex trigger on Contact and Lead object and also created helper class for Apex trigger to make callout to the MailChimp REST API. As per the Salesforce guidance we can not make callout through Apex trigger using normal apex method. To do callouts through Apex trigger we used @future annotation (future method) in Apex method.
    • This Solution/Approach works like this:
      • When someone checked ‘Email Opt Out’ checkbox on Contact/Lead record.
      • Trigger will fire and it makes callout to MailChimp API to unsubscribe the member from particular MailChimp list.
    • To Identify Subscribers from MailChimp list we need to have MD5 hash of the email address (member id), using this member id we can make any changes in the member on MailChimp using MailChimp REST API.
    • To get the email id in MD5 format we used Salesforce Crypto Class.
      Blob targetBlob = Blob.valueOf(emailId);
      Blob MD5hash = Crypto.generateDigest('MD5', targetBlob);
      String MD5MemeberId = EncodingUtil.convertToHex(MD5hash);
    • To process multiple records (bulkified records) we used MailChimp Batch Operations Endpoint to unsubscribe members from MailChimp. With batch operations we can complete more than one operation in just one call. We can use Batch Operation to add thousands of subscriber to list or Unsubscribe member from the list.
      Batch Operation Endpoint: https://XXXX.api.mailchimp.com/3.0/batches
  2. Apex Batch/ Scheduler Class:
    • We developed an Apex Batch class, Helper class and Scheduler class to fulfill the requirement. In the start method of an Apex batch all the Contact/Lead records on which ‘Email Opt Out’ is checked queried and execute method calls the Helper class method with email Id parameter to make a callout to MailChimp API, and ‘Unsubscribe’ the member from MailChimp list. The Apex Scheduler class is used to schedule the Batch to run at regular interval of time.
    • The Apex batch divided total number of Contact/Lead record in multiple chunks and process the chunks.
    • Same method (described in Approach 1) is used to get the email id in MD5 format.
    • We also used same MailChimp Batch Operation Endpoint (described in Approach 1).

Result:

  • The ‘MailChimp for Salesforce’ App doesn’t support above mentioned functionality. We developed custom solution (mentioned above) for the ‘Email Opt Out’ checkbox functionality.
  • When someone checked the ‘Email Opt Out’ checkbox on Contact/Lead record then that record will be Unsubscribed from MailChimp list.
  • When someone unchecked the ‘Email Opt Out’ checkbox on Contact/Lead record then that record will be Re-Subscribe in MailChimp list.
  • Client decided to go with first approach.