Marketo Merge Automatically with Zapier

Your duplicate list becoming ever more daunting? Stop the rot by learning how to build an "Automatic Marketo Merge" zap in Zapier that will merge duplicate leads as soon as they are created according to custom rules you specify.

Your duplicate list becoming ever more daunting? Stop the rot by learning how to build an “Automatic Marketo Merge” zap in Zapier that will merge duplicate leads as soon as they are created according to custom rules you specify.

Of course, the optimum approach to dealing with duplicates is to address the root causes that are causing duplicates to be created. The “Automatic Marketo Merge” zap is only intended to be a patch while you address the underlying issues creating duplicates.

If you are instead looking to deal with that daunting pile of duplicates in your instance and merge them all at once in bulk then take a look at the Marketo Merge Leads in Bulk using the API post to see how you can tackle this backlog using Python and the Marketo merge REST API endpoint.

All the Python code used in the “Code by Zapier” actions below can be found in the automatic_marketo_merge folder in Github.

Adding Duplicates to Static List in Marketo

A workaround is need to be able to trigger a Zap when duplicates are created in Marketo. As shown in the “Lead Added to Duplicates List Trigger” section below the “New Lead” trigger event of the Marketo Zapier app is used along with the constraint that the newly created lead must be a member of the “Possible Duplicates” static list.

Therefore in Marketo, every time a duplicate lead is created a smart campaign is used to transfer this lead to the “Possible Duplicates” static list and thereby trigger the “Automatic Marketo Merge” Zap to run.

Marketo smart campaign trigger for newly created duplicates
Marketo smart campaign trigger for newly created duplicates
Marketo smart campaign flow to add to the "Possible Duplicates" list
Marketo smart campaign flow to add to the “Possible Duplicates” list

Lead Added to Duplicates List Trigger

Once the “New Lead” trigger event has been selected in the Marketo Zapier app then add the “Possible Duplicates” static list to the “List” field.

New lead added to Marketo list trigger in Zapier
New lead added to Marketo list trigger in Zapier

Get Marketo Access Token

The “Code by Zapier” action is used along with Python code (get_marketo_token.py) to make a request to the Marketo REST API authentication endpoint to retrieve the access token needed to make subsequent requests.

"Code by Zapier" action using Python code to retrieve the Marketo access token
“Code by Zapier” action using Python code to retrieve the Marketo access token

Get Duplicate Leads

The “Lead Added to Duplicates List” trigger will only bring in one of the duplicate leads for a certain email address. This means that the leads endpoint of the Marketo REST API must be used to obtain all people in Marketo who share an email address (get_duplicate_leads.py).

The fields you want to compare between leads to choose winning values (Step 4: Select Winning Field Values) must be brought in in this request so make sure to modify the field_names string to include the fields you care about when merging e.g. leadSource, unsubscribed.

"Code by Zapier" action using Python to retrieve all leads in Marketo with the same email address
“Code by Zapier” action using Python to retrieve all leads in Marketo with the same email address

Select Winning Field Values

Once all duplicate leads sharing the same email address have been retrieved along with their field values for the fields you want to compare between them then the logic in the “Select Winning Field Values” Python code will decide which field values should win in the case of a difference (select_winning_values.py).

In general, if there are differences between field values Marketo’s inbuilt merging logic will choose the value of the winning lead provided that this value is not null. However, sometimes there are cases when you would like values on losing leads to be kept and this is where Python code can be used to specify your own custom rules to determine the field values that the merged lead should have.

Here are some examples of the logic implemented in this Python code:

  • The lead with an sfdcLeadID will be chosen as the winning lead over leads without an sfdcLeadID
  • The earliest createdAt date will win
  • A false value for the unsubscribed field will win over a true value (because you do not want to email this person if they have unsubscribed through one of the duplicates)
  • leadSource field values of Advertising, Paid Search, and Organic will be prioritized over values such as Sales Generated and Referral.

N.B. If you want more examples of prioritization rules you can use to pick winning values for different fields take a look at the “Rules for Winning Field Values” section of the Marketo Merge Leads in Bulk using the API.

You can modify the Python code in this step to prioritize values for each of the fields you care about when merging. Once the code has chosen winning field values these values are then stored and will be used in Step 6:Update Winning Lead to update these same fields on the winning lead to have these winning field values.

N.B. If you want to make sure that the Python logic has selected the correct winning lead id and winning field values before proceeding with the merge step then you can use the Zapier approval action to add a manual review before progressing.

Python code used to determine the winning values for each of the fields being compared on the duplicate leads
Python code used to determine the winning values for each of the fields being compared on the duplicate leads

Marketo Merge

A request is made to the Marketo merge endpoint to merge the duplicate leads with the winning lead determined from Step 4: Select Winning Field Values being specified as the winning id (marketo_merge.py).

To handle the scenario where there might be more than 2 duplicate leads sharing the same email address a for loop is used to successively merge the winning lead with each of the losers in turn. This is necessary because when the mergeinCRM parameter is set to true in the request to the Marketo merge endpoint only 2 leads can be merged at once.

N.B. Zapier has a 10-second timeout limit on all actions. When merging leads with the Marketo merge endpoint it takes more than 10 seconds for the merge to complete and the successful response to be sent to Zapier. Therefore, this Marketo merge action will most likely be marked as failed by Zapier even though the merge was successful. This is a limitation of Zapier and unfortunately, there is nothing to be done here.

"Code by Zapier" action using Python code to merge leads using the Marketo REST API
“Code by Zapier” action using Python code to merge leads using the Marketo REST API

Update Winning Lead

Once all the duplicates have been merged and the winning lead is left standing, this lead needs to have its fields updated with the winning values stored from Step 4: Select Winning Field Values. Ideally, we would check that the log returned from Step 5: Marketo Merge to see that the merge was successful before proceeding, however, as explained above, Zapier’s 10-second timeout on actions means that the Marketo merge step will most likely be marked as failed.

Therefore, Step 6:Update Winning Lead proceeds with the assumption that the merge was successful. If you want to ensure that the merge was successful before attempting an update then you can query the leads endpoint for each of the people involved in the merge to ensure that only the winning lead still exists.

A request is made to the leads endpoint to update the winning id designated from Step 4: Select Winning Field Values (update_winner.py). If the update is successful then the script and the zap both end.

However, sometimes Marketo’s inbuilt merging logic might have chosen a different winning id than that specified in Step 4: Select Winning Field Values. This can happen in the case that the one of the leads being merged is a contact in SFDC and the others are leads. Marketo’s inbuilt merging logic will always choose the contact to be the winner in this case.

If the Python logic used in Step 4: Select Winning Field Values selects an SFDC lead as the winner then Marketo will override this choice and the SFDC contact will be the actual winner. Therefore, when attempting to update the incorrectly labeled winning id from Step 4: Select Winning Field Values this lead will no longer exist and the keyword “skipped” will be in the update response.

This is why a while loop is used to cycle through the other ids that were part of the merge until the actual winner is found and updated.

You can avoid the need for this while loop and ensure that the winner selected in your Python logic always matches that of Marketo’s inbuilt logic. This can be done by modifying Step 3: Get Duplicate Leads to bring in the sfdcType field and then modifying the Python code in Step 4: Select Winning Field Values to ensure that an SFDC contact will always be selected as the winner if the other people to be merged are SFDC leads.

"Code by Zapier" action to update the winning lead with the winning field values
“Code by Zapier” action to update the winning lead with the winning field values

Next Up: Marketo Merge in Bulk with the API

Now that you have mastered how to use Zapier and the Marketo merge REST API endpoint to merge leads automatically upon creation, what do you do with that huge backlog of duplicates?

I have just the post for you! The Marketo Merge Leads in Bulk using the API will show you how to use the exact same Marketo merge endpoint to merge leads in bulk using Python to make the request and to set custom rules for how you want to prioritize conflicting values for the same fields on different leads.

Also if you want to merge with more confidence take a look at the Zapier Approval Workflow Example with Slack Alerts to see how to add a manual review step to this “automatic Marketo merge” zap to ensure that the correct winning lead id and field values have been selected before merging.


Automating with APIs, Python, Zapier, and Google Scripts 

Former engineer turned marketing operations lead who loves to get down and dirty with the Marketo API, Python, Zapier, and Google Scripts.

Post a comment