Skip to content

Instantly share code, notes, and snippets.

@chrisoklepke
Created October 17, 2023 14:53
Show Gist options
  • Save chrisoklepke/562a655dbcd1550ded638543bdd16f45 to your computer and use it in GitHub Desktop.
Save chrisoklepke/562a655dbcd1550ded638543bdd16f45 to your computer and use it in GitHub Desktop.
"""
This custom code action is intended to solve the problem when a user is entering two different
email adresses in the configurator and the chargebee dropin. As a result, two contacts are created.
One has the custom objects and the other one has the deal associated.
Buy looking for the Hash of the Deal of contacts with a Deal but without custom objects,
we determin the other contact ID and merge them together.
"""
import os
import requests
from hubspot import HubSpot
from hubspot.crm.contacts import ApiException as ContactApiException
# from hubspot.crm.deals import ApiException as DealApiException
from hubspot.crm.objects import ApiException as ObjectApiException
def main(event):
hubspot = HubSpot(api_key=os.getenv("HAPIKEYsandbox"))
prim_contact_id = event.get("object").get("objectId")
try:
get_assoc = hubspot.crm.contacts.associations_api.get_all(
contact_id=prim_contact_id, to_object_type="DEAL"
)
deal_id = get_assoc.results[0].id
except ContactApiException as exception:
print(f"Exception when calling associations_api->get_all: {exception}")
# try:
# get_deal = hubspot.crm.deals.basic_api.get_by_id(
# deal_id=deal_id, properties=["cb_subcst_cf_deal_hash"], archived=False
# )
# deal_hash = get_deal.properties["cb_subcst_cf_deal_hash"]
# except DealApiException as exception:
# print(f"Exception when calling basic_api->get_by_id: {exception}")
try:
get_assoc_hardware_set = hubspot.crm.objects.associations_api.get_all(
object_type="DEAL", object_id=deal_id, to_object_type="2-104422609"
)
hardware_set_id = get_assoc_hardware_set.results[0].id
except ObjectApiException as exception:
print(f"Exception when calling associations_api->get_all: {exception}")
try:
get_assoc_contact = hubspot.crm.objects.associations_api.get_all(
object_type="2-104422609",
object_id=hardware_set_id,
to_object_type="CONTACT",
)
sec_contact_id = get_assoc_contact.results[0].id
except ObjectApiException as exception:
print(f"Exception when calling associations_api->get_all: {exception}")
url = (
f"https://api.hubapi.com/contacts/v1/contact/merge-vids/{prim_contact_id}/?hapikey="
+ os.getenv("HAPIKEYsandbox")
)
post_body = {"vidToMerge": sec_contact_id}
headers = {"Content-Type": "application/json"}
merge_contacts = requests.request("POST", url, headers=headers, json=post_body)
print(merge_contacts.text)
return
payload = {
"origin": {
# Your portal ID
"portalId": 1,
# Your custom action definition ID
"actionDefinitionId": 8601,
},
"object": {
# The type of CRM object that is enrolled in the workflow
"objectType": "CONTACT",
# The ID of the CRM object that is enrolled in the workflow
"objectId": 8601,
},
"inputFields": {
# The property name for defined inputs
"object_hash": "12345678908765432"
},
# A unique ID for this execution
"callbackId": "ap-123-456-7-8",
}
main(payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment