Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workflow automation on contact duplicates #610

Open
AnthonyRollins opened this issue Jan 16, 2025 · 1 comment
Open

Workflow automation on contact duplicates #610

AnthonyRollins opened this issue Jan 16, 2025 · 1 comment

Comments

@AnthonyRollins
Copy link
Member

Create new Hubspot workflows to support streamlining data maintenance for Contacts and Company records.

Using a Hubspot template as the base to create a workflow that does the following:

  • For Contact: Lookup email addresses, and if there are duplicate,s automatically merge records
  • For Company: Lookup company domain names, and if there are duplicates, automatically merge records
@AnthonyRollins
Copy link
Member Author

It appears they are depreciating code language NODE16X used for the template Image

Our only other option is to use Python 3.9 would the following code below have to be updated to accommodate the new language?

/**
 * This custom code action removes duplicate contacts based on a specific contact property
 */

// Import required libraries
const hubspot = require('@hubspot/api-client');

/**
 * How to access HubSpot's client to make API
 * 1) Create a Private App: https://developers.hubspot.com/docs/api/private-apps
 * 2) Create a Secret using your Private App's access token via the "Secrets" dropdown above
 * 3) Insert your secret below, replacing "YOUR_PRIVATE_APP_ACCESS_TOKEN" with the name of your Secret
 */
const HUBSPOT_PRIVATE_APP_ACCESS_TOKEN = process.env.YOUR_PRIVATE_APP_ACCESS_TOKEN;

// By default, phone numbers are used to dedupe contacts (i.e. 2 contacts with the same phone number are merged together)
// Change this property if you would like to use a different property to dedupe contacts with 
const PROPERTY_USED_TO_DEDUPE_CONTACTS = 'email';

exports.main = (event, callback) => {
  const hubspotClient = new hubspot.Client({ accessToken: HUBSPOT_PRIVATE_APP_ACCESS_TOKEN });

  hubspotClient.crm.contacts.basicApi
    .getById(event.object.objectId, [PROPERTY_USED_TO_DEDUPE_CONTACTS])
    .then(contactResult => {
      let dedupePropValue = contactResult.properties[PROPERTY_USED_TO_DEDUPE_CONTACTS];

      console.log(`Looking for duplicates based on ${PROPERTY_USED_TO_DEDUPE_CONTACTS} = ${dedupePropValue}`);
      hubspotClient.crm.contacts.searchApi.doSearch({
          filterGroups: [{
            filters: [{
              propertyName: PROPERTY_USED_TO_DEDUPE_CONTACTS,
              operator: 'EQ',
              value: dedupePropValue
            }]
          }]
        })
        .then(searchResults => {
          let idsToMerge = searchResults.results
            .map(object => object.id)
            .filter(vid => Number(vid) !== Number(event.object.objectId));

          if (idsToMerge.length == 0) {
            console.log('No matching contact, nothing to merge');
            return;
          } else if (idsToMerge.length > 1) {
            console.log(`Found multiple potential contact IDs ${idsToMerge.join(', ')} to merge`);
            throw new Error("Ambiguous merge; more than one matching contact");
          }

          let idToMerge = idsToMerge[0];
          console.log(`Merging enrolled contact id=${event.object.objectId} into contact id=${idToMerge}`);
          hubspotClient
            .apiRequest({
              method: 'POST',
              path: `/contacts/v1/contact/merge-vids/${idToMerge}`,
              body: {
                vidToMerge: event.object.objectId
              }
            })
            .then(mergeResult => {
              console.log('Contacts merged!');
            });
        });
    });
};

Here's the default language that appeared when I switched to Python 3.9

def main(event):
  # Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
  email = event["inputFields"]["email"]
  # Return the output data that can be used in later actions in your workflow.
  return {
    "outputFields": {
      "email": email
    }
  }

@o-ram o-ram moved this from Todo to Needs research in Customer Success Jan 25, 2025
@AnthonyRollins AnthonyRollins changed the title Workflow automations in Hubspot Workflow automation on contact duplicates Jan 28, 2025
@AnthonyRollins AnthonyRollins moved this from Needs research to Todo in Customer Success Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

1 participant