-
Notifications
You must be signed in to change notification settings - Fork 5.5k
[#18193] - Hubspot Contact added to list trigger
#18194
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
Merged
vunguyenhung
merged 12 commits into
PipedreamHQ:master
from
certifier-me:18193-hubspot-contact-added-to-list-trigger
Sep 9, 2025
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
46df316
New contact added to list trigger
choeqq ee9b575
bump hubspot version
choeqq a3c7889
cr 1
choeqq 94d42f7
processing by lastMembershipTimestamp
choeqq b172bdf
Merge branch 'master' into 18193-hubspot-contact-added-to-list-trigger
choeqq 3585f78
enhance id with membershipTimestamp
choeqq fa48fdb
remove data chunking
choeqq d0923d8
cr 2
choeqq 682d46b
cr 2
choeqq 3c6c7c6
Merge branch 'master' into 18193-hubspot-contact-added-to-list-trigger
choeqq 6d5b15e
Merge branch 'master' into 18193-hubspot-contact-added-to-list-trigger
choeqq 099fa4d
bump versions
choeqq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
components/hubspot/sources/new-contact-added-to-list/new-contact-added-to-list.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| import common from "../common/common.mjs"; | ||
| import { | ||
| DEFAULT_LIMIT, | ||
| DEFAULT_CONTACT_PROPERTIES, | ||
| } from "../../common/constants.mjs"; | ||
| import sampleEmit from "./test-event.mjs"; | ||
|
|
||
| export default { | ||
| ...common, | ||
| key: "hubspot-new-contact-added-to-list", | ||
| name: "New Contact Added to List", | ||
| description: | ||
| "Emit new event when a contact is added to a HubSpot list. [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/lists#get-%2Fcrm%2Fv3%2Flists%2F%7Blistid%7D%2Fmemberships%2Fjoin-order)", | ||
| version: "0.0.1", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| props: { | ||
| ...common.props, | ||
| info: { | ||
|
Check warning on line 19 in components/hubspot/sources/new-contact-added-to-list/new-contact-added-to-list.mjs
|
||
| type: "alert", | ||
| alertType: "info", | ||
| content: `Properties:\n\`${DEFAULT_CONTACT_PROPERTIES.join(", ")}\``, | ||
| }, | ||
| listId: { | ||
| propDefinition: [ | ||
| common.props.hubspot, | ||
| "listId", | ||
| ], | ||
| type: "string", | ||
| description: "Select the list to watch for new contacts.", | ||
| optional: false, | ||
| }, | ||
choeqq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| properties: { | ||
| propDefinition: [ | ||
| common.props.hubspot, | ||
| "contactProperties", | ||
| () => ({ | ||
| excludeDefaultProperties: true, | ||
| }), | ||
| ], | ||
| label: "Additional contact properties to retrieve", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| methods: { | ||
| ...common.methods, | ||
| _getKey(listId) { | ||
| return `list_${listId}_last_timestamp`; | ||
| }, | ||
| _getLastMembershipTimestamp(listId) { | ||
| return this.db.get(this._getKey(listId)); | ||
| }, | ||
| _setLastMembershipTimestamp(listId, timestamp) { | ||
| this.db.set(this._getKey(listId), timestamp); | ||
| }, | ||
| getTs() { | ||
choeqq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return Date.now(); | ||
| }, | ||
| generateMeta(membership, listInfo) { | ||
| const { | ||
| recordId, membershipTimestamp, | ||
| } = membership; | ||
| const ts = membershipTimestamp | ||
| ? new Date(membershipTimestamp).getTime() | ||
| : this.getTs(); | ||
|
|
||
| return { | ||
| id: `${listInfo.listId}-${recordId}-${ts}`, | ||
| summary: `Contact ${recordId} added to list: ${listInfo.name}`, | ||
| ts, | ||
| }; | ||
| }, | ||
| async getContactDetails(contactIds) { | ||
| if (!contactIds.length) return {}; | ||
|
|
||
| const { properties = [] } = this; | ||
| const allProperties = [ | ||
| ...DEFAULT_CONTACT_PROPERTIES, | ||
| ...properties, | ||
| ]; | ||
|
|
||
| const chunks = []; | ||
| const chunkSize = 100; | ||
| for (let i = 0; i < contactIds.length; i += chunkSize) { | ||
| chunks.push(contactIds.slice(i, i + chunkSize)); | ||
| } | ||
|
|
||
| const contactMap = {}; | ||
|
|
||
| try { | ||
| for (const chunk of chunks) { | ||
| try { | ||
| const { results } = await this.hubspot.batchGetObjects({ | ||
| objectType: "contacts", | ||
| data: { | ||
| inputs: chunk.map((id) => ({ | ||
| id, | ||
| })), | ||
| properties: allProperties, | ||
| }, | ||
| }); | ||
|
|
||
| results.forEach((contact) => { | ||
| contactMap[contact.id] = contact; | ||
| }); | ||
| } catch (error) { | ||
| console.warn( | ||
| `Error fetching contact details for chunk of ${chunk.length} contacts:`, | ||
| error, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return contactMap; | ||
| } catch (error) { | ||
| console.warn("Error processing contact details:", error); | ||
| return {}; | ||
| } | ||
| }, | ||
| async processListMemberships(listId, listInfo) { | ||
| const lastMembershipTimestamp = this._getLastMembershipTimestamp(listId); | ||
| const newMemberships = []; | ||
|
|
||
| let params = { | ||
| limit: DEFAULT_LIMIT, | ||
| }; | ||
|
|
||
| try { | ||
| let hasMore = true; | ||
| let latestMembershipTimestamp = lastMembershipTimestamp; | ||
|
|
||
| if (!lastMembershipTimestamp) { | ||
| const baselineTimestamp = new Date().toISOString(); | ||
| this._setLastMembershipTimestamp(listId, baselineTimestamp); | ||
| return newMemberships; | ||
| } | ||
|
|
||
| while (hasMore) { | ||
| const { | ||
| results, paging, | ||
| } = | ||
| await this.hubspot.getListMembershipsByJoinOrder({ | ||
| listId, | ||
| params, | ||
| }); | ||
|
|
||
| if (!results) { | ||
| console.warn( | ||
| `No results returned from API for list ${listId} - possible API issue`, | ||
| ); | ||
| break; | ||
| } | ||
|
|
||
| if (results.length === 0) { | ||
| break; | ||
| } | ||
|
|
||
| for (const membership of results) { | ||
| const { membershipTimestamp } = membership; | ||
|
|
||
| if (membershipTimestamp > lastMembershipTimestamp) { | ||
| newMemberships.push({ | ||
| membership, | ||
| listInfo, | ||
| }); | ||
|
|
||
| if ( | ||
| !latestMembershipTimestamp || | ||
| membershipTimestamp > latestMembershipTimestamp | ||
| ) { | ||
| latestMembershipTimestamp = membershipTimestamp; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (paging?.next?.after) { | ||
| params.after = paging.next.after; | ||
| } else { | ||
| hasMore = false; | ||
| } | ||
| } | ||
|
|
||
| if (latestMembershipTimestamp !== lastMembershipTimestamp) { | ||
| this._setLastMembershipTimestamp(listId, latestMembershipTimestamp); | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error processing list ${listId}:`, error); | ||
| } | ||
|
|
||
| return newMemberships; | ||
| }, | ||
| async processResults() { | ||
| const { | ||
| listId, | ||
| listInfo: { name }, | ||
| } = this; | ||
|
|
||
| if (!listId) { | ||
| console.warn("No list selected to monitor"); | ||
| return; | ||
| } | ||
|
|
||
| const listInfo = { | ||
| listId, | ||
| name: `List ${name}`, | ||
| }; | ||
choeqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
michelle0927 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try { | ||
| const newMemberships = await this.processListMemberships( | ||
| listId, | ||
| listInfo, | ||
| ); | ||
|
|
||
| if (newMemberships.length > 0) { | ||
| const contactIds = newMemberships.map( | ||
| ({ membership }) => membership.recordId, | ||
| ); | ||
| const contactDetails = await this.getContactDetails(contactIds); | ||
|
|
||
| for (const { | ||
| membership, listInfo, | ||
| } of newMemberships) { | ||
| const contactDetail = contactDetails[membership.recordId] || {}; | ||
|
|
||
| const eventData = { | ||
| listId: listInfo.listId, | ||
| listName: listInfo.name, | ||
| contactId: membership.recordId, | ||
| contact: contactDetail, | ||
| membership, | ||
| addedAt: membership.membershipTimestamp, | ||
| }; | ||
|
|
||
| const meta = this.generateMeta(membership, listInfo); | ||
| this.$emit(eventData, meta); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error(`Error processing list ${listId}:`, error); | ||
| } | ||
| }, | ||
| getParams() { | ||
| return {}; | ||
| }, | ||
| }, | ||
| sampleEmit, | ||
| }; | ||
43 changes: 43 additions & 0 deletions
43
components/hubspot/sources/new-contact-added-to-list/test-event.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| export default { | ||
| listId: "123456789", | ||
| listName: "List 123456789", | ||
| contactId: "31612976545", | ||
| contact: { | ||
| id: "31612976545", | ||
| properties: { | ||
| address: null, | ||
| annualrevenue: null, | ||
| city: "San Francisco", | ||
| company: "Acme Corp", | ||
| country: "United States", | ||
| createdate: "2024-08-26T15:30:45.123Z", | ||
| email: "[email protected]", | ||
| fax: null, | ||
| firstname: "John", | ||
| hs_createdate: "2024-08-26T15:30:45.123Z", | ||
| hs_email_domain: "example.com", | ||
| hs_language: null, | ||
| hs_object_id: "31612976545", | ||
| hs_persona: null, | ||
| industry: "Technology", | ||
| jobtitle: "Software Engineer", | ||
| lastmodifieddate: "2024-08-26T15:32:15.456Z", | ||
| lastname: "Doe", | ||
| lifecyclestage: "lead", | ||
| mobilephone: null, | ||
| numemployees: null, | ||
| phone: "+1-555-123-4567", | ||
| salutation: null, | ||
| state: "California", | ||
| website: "https://example.com", | ||
| zip: "94102", | ||
| }, | ||
| createdAt: "2024-08-26T15:30:45.123Z", | ||
| updatedAt: "2024-08-26T15:32:15.456Z", | ||
| archived: false, | ||
| }, | ||
| membership: { | ||
| recordId: "31612976545", | ||
| }, | ||
| addedAt: "2024-08-26T15:35:20.789Z", | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.