-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Code
More file actions
140 lines (111 loc) · 5.19 KB
/
Python Code
File metadata and controls
140 lines (111 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import requests
import json
# HubSpot setup
HUBSPOT_ACCESS_TOKEN = os.getenv("workflow_key")
HEADERS = {
"Authorization": f"Bearer {HUBSPOT_ACCESS_TOKEN}",
"Content-Type": "application/json"
}
# Configuration
DEAL_PROPERTY = "multi_deal_prop"
COMPANY_PROP_1 = "company_multi_prop_1"
COMPANY_PROP_2 = "company_multi_prop_2"
# Define mapping from deal option values to company property values
# Map the deal values to the correct company property internal values
MAPPINGS = {
# Deal values -> company_multi_prop_1 values
"1": {"property": COMPANY_PROP_1, "value": "1"}, # Map deal "1" to company prop 1 value "1"
"2": {"property": COMPANY_PROP_1, "value": "2"}, # Map deal "2" to company prop 1 value "2"
"3": {"property": COMPANY_PROP_1, "value": "3"}, # Map deal "3" to company prop 1 value "3"
"4": {"property": COMPANY_PROP_1, "value": "4"}, # Map deal "4" to company prop 1 value "4"
# Deal values -> company_multi_prop_2 values
"5": {"property": COMPANY_PROP_2, "value": "1"}, # Map deal "5" to company prop 2 value "1"
"6": {"property": COMPANY_PROP_2, "value": "2"}, # Map deal "6" to company prop 2 value "2"
"7": {"property": COMPANY_PROP_2, "value": "3"}, # Map deal "7" to company prop 2 value "3"
"8": {"property": COMPANY_PROP_2, "value": "4"}, # Map deal "8" to company prop 2 value "4"
}
def get_deal_properties(deal_id):
"""Get the deal properties we need"""
url = f"https://api.hubapi.com/crm/v3/objects/deals/{deal_id}?properties={DEAL_PROPERTY}"
response = requests.get(url, headers=HEADERS)
if response.status_code != 200:
print(f"Error fetching deal: {response.status_code} - {response.text}")
return None
return response.json()["properties"]
def get_associated_companies(deal_id):
"""Get companies associated with this deal"""
url = f"https://api.hubapi.com/crm/v3/objects/deals/{deal_id}/associations/companies"
response = requests.get(url, headers=HEADERS)
if response.status_code != 200:
print(f"Error fetching associations: {response.status_code} - {response.text}")
return []
results = response.json().get("results", [])
return [result["id"] for result in results]
def update_company_properties(company_id, selected_values):
"""Update the company properties with the mapped values"""
url = f"https://api.hubapi.com/crm/v3/objects/companies/{company_id}"
# Group values by target property
property_values = {}
for deal_value in selected_values:
if deal_value in MAPPINGS:
mapping = MAPPINGS[deal_value]
target_property = mapping["property"]
company_value = mapping["value"]
if target_property not in property_values:
property_values[target_property] = []
property_values[target_property].append(company_value)
# Format properties for update
properties = {}
for prop, values in property_values.items():
properties[prop] = ";".join(values)
if not properties:
print("No valid property mappings found")
return True # Nothing to update
data = {
"properties": properties
}
print(f"Updating company {company_id} with properties: {json.dumps(properties)}")
response = requests.patch(url, headers=HEADERS, json=data)
if response.status_code == 200:
print(f"Updated company {company_id} with mapped properties.")
return True
else:
print(f"Error updating company {company_id}: {response.status_code} - {response.text}")
return False
def main(event):
# Get the deal ID from the current workflow execution
deal_id = event["inputFields"]["hs_object_id"]
print(f"Starting property mapping for deal {deal_id}")
# Get the deal properties
deal_properties = get_deal_properties(deal_id)
if not deal_properties:
print("Could not fetch deal properties.")
return {"outputFields": {"mapping_success": False}}
# Get the multi-select property value
deal_property_value = deal_properties.get(DEAL_PROPERTY)
if not deal_property_value:
print(f"Deal does not have the {DEAL_PROPERTY} property set.")
return {"outputFields": {"mapping_success": False}}
# Handle multi-select values (semicolon separated)
selected_values = deal_property_value.split(";")
print(f"Found selected values: {selected_values}")
# Get associated companies
company_ids = get_associated_companies(deal_id)
if not company_ids:
print("No companies associated with this deal.")
return {"outputFields": {"mapping_success": False}}
print(f"Found {len(company_ids)} associated companies.")
# Update each associated company
success_count = 0
for company_id in company_ids:
if update_company_properties(company_id, selected_values):
success_count += 1
success = success_count > 0
print(f"Mapping complete. Successfully updated {success_count} of {len(company_ids)} companies.")
return {
"outputFields": {
"mapping_success": success,
"companies_updated": success_count
}
}