Created
March 5, 2022 12:27
-
-
Save chrisoklepke/6a2a81b47be1843efb0482a1ae0d18bb to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
from pprint import pprint | |
from hubspot import HubSpot | |
def main(event): | |
# Change the Object Type and Association Type to your custom object | |
customObjectType = "2-XXXXXXXXX" | |
associationType = "contact_to_CUSTOMOBJECT" | |
hubspot = HubSpot(api_key=os.getenv("HAPIKEY")) | |
# Get contact ID of associated contact of deal | |
contactId = hubspot.crm.deals.associations_api.get_all( | |
deal_id=event.get("object").get("objectId"), | |
to_object_type="CONTACT" | |
).results[0].id | |
pprint("The associated contact ID:") | |
pprint(contactId) | |
# Get the associated custom objects of deal | |
customObjects = hubspot.crm.deals.associations_api.get_all( | |
deal_id=event.get("object").get("objectId"), | |
to_object_type=customObjectType | |
).results | |
pprint("The custom objects are:") | |
pprint(customObjects) | |
# Associate the contact to each custom objects | |
for object in customObjects: | |
customObjectId = object.id | |
pprint("custom object ID:") | |
pprint(customObjectId) | |
associateContactToCustomObject = hubspot.crm.contacts.associations_api.create( | |
contact_id=contactId, | |
to_object_type=customObjectType, | |
to_object_id=customObjectId, | |
association_type=associationType | |
) | |
pprint("Response for association:") | |
pprint(associateContactToCustomObject) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment