SFMC
Marketing Cloud

Using REST API to delete a contact from the Marketing Cloud Account

In today’s digital landscape, managing customer data efficiently is crucial for successful marketing campaigns. Salesforce Marketing Cloud provides robust APIs that allow developers to interact with contact data programmatically. In this blog post, we’ll explore how to use the REST API in CRM to delete contacts from your Marketing Cloud account. Whether you’re cleaning up outdated records or converted leads records, understanding the process is essential. Let’s dive in!

Use case:

  • When lead records are populated in SFMC’s synchronized data extension, a _ContactKey field is added to store the LeadId or Converted ContactId.
  • If an email is sent to a subscriber using the lead which syncs from CRM to SFMC (either by using filter data extensions or using a query to store the lead data in a standard data extension).
  • A record is created with SubscriberKey (LeadId), and EmailAddress in the All Subscriber list in SFMC.
  • If the same lead is converted, the _ContactKey attribute will be changed to Converted ContactId.
  • Suppose an email is sent to the same Lead, in that case, a new record will be created with a new SubscriberKey (Converted ContactId) but the subscriber record with SubscriberKey (LeadId) will not be deleted.
  • At the same time in CRM, once a lead is converted all the Individual Email Result records will be archived with the lead record without transferring all of it to the converted contact.

Prerequisite :

  1. Should have Salesforce CRM Enterprise or above edition.
  2. Salesforce CRM should be integrated to SFMC using Marketing Cloud Connect.

Problem Statement :

  1. Minimum 2 subscriber records will be saved in all subscriber and in all contacts when a email is sent before and after to a lead.
  2. Consumes contact storage in the long run.
  3. Lead IERs are archived after the lead

After the solution implementation :

  1. No duplication of subscriber data in SFMC.
  2. Saves contact storage in SFMC.
  3. Historic IERs from lead will be transferred to contact to be accessible.

Solution:

A flow will be created in CRM that triggers when a lead is converted and transfers all the Individual Email Results to Contact, while asynchronously deleting the lead from the SFMC account while creating an auth connection using a package in SFMC, it’s a complete codeless solution, that uses the following elements-

  1. Flow Builder (CRM).
  2. Named Credentials (CRM).
  3. External Credentials (CRM).
  4. A permission set with Named and External credential access (CRM) Integration Package (SFMC)

Steps to the Solution:

  1. Create a package in SFMC for authentication with credentials.
    1. Go to Setup -> Platform Tools -> Apps -> Installed Packages.
    2. Click on New.
    3. Add a Name and description of your choice in the “New Package Details”.

      MarketingCloud

    4. Add all the permissions needed (depending on how and what the package will be used for), for now, add delete permission for contacts and click on Save.

      MarketingCloud

      NOTE: Create the package in the Business Unit connected with the targeted CRM instance.

  2. Turn on the Contact Deletion in SFMC.
    1. Go to Contact Builder > Contacts Configuration.
    2. Click on the “On” toggle under the “Contact Delete” section, then select 1 or 0 in the “Contact Deletion Settings” drop-down, and click on Save.

      MarketingCloud

      This will allow the contact deletion throughout the SFMC Account

  3. Create an External credential in SFMC.
    1. Go to Setup > Security > Named Credentials > External Credentials.

      MarketingCloud

    2. Click on the New button and add the details as shown in the image below (get the Auth URL from SFMC’s Package that was created in step 1), and click on Save.

      MarketingCloud

  4. Create Named Credential to be used in the flow for authentication.
    1. Go to Setup > Security > Named Credentials, and click on the “New” button.

      MarketingCloud

    2. Add all the details as shown in the below image sample, get the REST URI form the package created in step 1, and only replace the “YOUR_REST_URL” with the key provided in the package URI. Add “External Credential” and “Client Certificate” that was created in previous steps.

      MarketingCloud

    3. Tick all the options under Callout Options and click on the Save button.

      MarketingCloud

  5. Create a Permission Set to give access to the user for External Credentials.
  6. Create a record-triggered Flow on the Lead object that triggers when a lead is converted.
    Add the IER transfer logic in the flow to run immediately and SFMC Lead delete logic will be on the Asynchronous path, which will include apex action for http callout.

    MarketingCloud

In results, without writing code this will delete Converted Lead in SFMC and transfer all the Individual Email Results to Converted Contact in CRM.

Hope this helps, please do share and comment.

Leave a Reply