Business Problem – Campaign Member Deletion Automation

MailChimp is a great tool for people who are just starting out with email marketing and they currently use Salesforce as their CRM. We have done several implementations of MailChimp and Salesforce and wanted to provide you with our best practice approach of how to set it up right the first time.

This is Part 4 of how to setup the MailChimp to Salesforce integration where we will show you how to solve the following business challenge we ran into with implementing MailChimp for Salesforce. Check out MailChimp to Salesforce Integration – Part 1 and MailChimp to Salesforce Integration – Part 2 for additional tips.

  • Delete Campaign Member from Salesforce Campaign Automation – When a MailChimp subscriber unsubscribes from an audience, we want to delete the campaign member from a Campaign in Salesforce.

How to Instructions

Delete Campaign Member from Salesforce Campaign

Our objective here is to automatically delete the Campaign Member from a Salesforce Campaign when a MailChimp Subscriber unsubscribes from an audience in MailChimp. If you completed the steps in MailChimp to Salesforce Integration – Part 2 you already have the process builder in place that we will build off of for this automation. If you have not, please review this how to and setup the process builder with the 1st set of criteria for MC Member Status = Unsubscribed.

  • First step is to create an Autolaunched Flow, go to Setup | Flows | New Flow | Autolaunched Flow  Here is what the final flow will look like. We will go through each element in detail so you can set this up yourself.
  • Next create two text variables, to create a text variable go to the Manager tab in the Toolbox | Click New Resource button. Create two variables called varEmailAddress and varMCSubscriberId. Later you will see how we pass the values for these variables from the process builder to the flow.
  • Next we need to create two Get Records elements, one for Contacts and the other for Campaign Members. Go to the Elements tab | Drag Get Records Element onto the canvas.
    1. Get Contacts – this element is looking for all of the Contacts that have a matching MC Subscriber Id. Remember, you could have more than one Contact in Salesforce with the same email address, so there is a one to many relationship between an MC Subscriber Id and a Contact. Multiple contacts can have the same MC Subscriber Id. The Get Contacts element needs to look for All Records, not just the first record.
    2. Get Campaign Members – this element is looking for all of the Campaign Members with the MC Subscriber email address. For the same reason as above, this element also needs to look for All Records, not just the first record.
  • Now drag a Loop element onto the canvas. We need to loop through the Contact records found in the Get Contacts element so we can add them to a list to use later in the flow.
  • Create a new text collection variable called varContactIdList – make sure to click the checkbox Allow multiple values (collection)
  • Drag an Assignment element onto the canvas. Configure it like this: {!varContactIdList} Add {!Loop_Contacts.Id} This will add the current Loop variable Id to the varContactIdList variable you created.
  • Drag another Loop element onto the canvas to loop over Get Campaign Members. Since we have a many to many relationship between Contacts and Campaign Members, we need to use two Loops. You can see we made sure to do the get records queries outside of our loops. It is best practice to keep all queries and updates outside of your loops when configuring flows.
  • Next, we need to create a Custom Label to store the values of the Campaign Ids we want to use in the flow. This allows us to update the Campaign Id values without having to go into the process builder to make the change. Go to Setup | Custom Label | Click New Custom Label
    • Enter the values of the Campaign Ids into the Value field.
  • Drag a Decision element onto the canvas. Configure the Yes path like this:
    • {!varContactIdList} contains {!Loop_Campaign_Members.ContactId}.
    • {!$Label.MCSubscriberCampaignDelete} contains {!Loop_Campaign_Members.CampaignId}
  • This will check if the varContactIdList contains the value of the current Loop variable’s Campaign Member Id. We updated the Default Outcome to be called No.
  • Create a new Record Variable called varCampaignMemberRecord. This variable will store the Id of the Campaign Member record so we can add them to a collection. Make sure Data Type = Record.
  • Drag an Assignment element onto the canvas. Configure it like this: {!varCampaignMemberRecord.Id} = {!Loop_Campaign_Members.Id}  This is assigning the Campaign Member Id of the current record in the loop to the Campaign Member Id in the new record variable you created.
  • Create a Record Collection variable called CollectCampaignMemberstoDelete – make sure Data Type = Record and the Allow multiple values (collection) checkbox is checked.
  • Drag another Assignment variable onto the canvas. Configure it like this: {!CollectCampaignMembersToDelete} Add {!varCampaignMemberRecord} This will add the Campaign Member records to a collection so we can delete them.
  • Now make cure to connect the arrows correctly for the Decision element. The No path should go back to the Loop Campaign Members element. The Yes path should go to the Assign Campaign Member Id element.
  • Drag a Delete Records element onto the canvas. Select Records to Delete = CollectCampaignMemberstoDelete collection variable.
  • Now is a good time to test the flow before we configure the process builder to make sure the flow works. Here are the steps I used to test it:
    • Create two contact records with the same email address in Salesforce. Make sure the email address for these contacts is also a MailChimp Subscriber
    • Create a Salesforce Campaign and add these two contacts to the Campaign.
    • Find the MC Subscriber Id of your Contacts by using the Developer Console. You may have a better way to do this, let me know!
      1. Click the Query Editor in the Developer Console
      2. Enter this query Select Id, MC4SF__Email2__c from MC4SF__MC_Subscriber__c where MC4SF__Email2__c = ‘youremailaddresshere’
      3. The Id of the MC Subscriber is shown in the Query results. You should only get one record.
    • Make sure your Custom Label has the Campaign Id in the value field.
    • Click the Debug on Canvas button in the flow. Enter the two input variables: Email Address and MC Subscriber ID. Click Run.
    • Confirm the test followed the correct paths in the flow. Check the Campaign to make sure the records were deleted.
    • If everything worked, click Activate – we are ready to configure the process builder!
  • Now, go to the Process Builder you created from Part 2 of this how to series. Go to the 1st criteria that is looking for the Member Status of the MC Subscriber record to change to Unsubscribed.
  • Click Add Action and select Flows
  • Select the MC Subscriber | Delete Campaign Member flow we created above.
  • Configure the variables like this:
    • varMCSubscriberID field reference [MC4SF__MC_Subscriber__c].Id
    • varEmailAddress field reference [MC4SF__MC_Subscriber__c].MC4SF__Email2__c
  • Click Activate – we are ready to test it!

How to Test the Process Builder Invoking the Flow

  • Create two contact records with the same email address in Salesforce. Make sure the email address for these contacts is also an active MailChimp Subscriber.
  • Create a Salesforce Campaign and add these two contacts to the Campaign.
  • Add the Campaign Id to the Custom Label
  • Confirm the Process Builder and Flow are both Active
  • Go to one of the Contact pages and click Unsubscribe from the MailChimp subscriptions section.
  • Check your campaign and confirm the Contacts were deleted from the Campaign. If they are gone, congratulations!! Now you are ready to let it rip with the entire audience!