I am trying to assign sales rep territory based on state and/or country using hubdb table and custom code workflow and the workflow is not finding state or country for the contacts can you please help
import requests
# Your HubSpot access token
ACCESS_TOKEN = ‘pat-na1-b2c90668-48f8-4d16-a294-9decb144cee8’
# HubDB table ID
TABLE_ID = ‘104974652’
# Define the headers for the requests
HEADERS = {
‘Authorization’: f’Bearer {ACCESS_TOKEN}',
‘Content-Type’: ‘application/json’
}
# Fetch data from HubDB table
def get_hubdb_data():
url = f’https://api.hubapi.com/hubdb/api/v2/tables/{TABLE_ID}/rows’
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
print(f"Fetched {len(data[‘objects’])} rows from HubDB") # Debug statement
return data[‘objects’]
# Fetch contacts from HubSpot
def get_contacts():
url = ‘https://api.hubapi.com/contacts/v1/lists/all/contacts/all’
response = requests.get(url, headers=HEADERS)
response.raise_for_status()
data = response.json()
print(f"Fetched {len(data[‘contacts’])} contacts from HubSpot") # Debug statement
return data[‘contacts’]
# Update contact property
def update_contact_property(contact_id, property_name, property_value):
url = f’https://api.hubapi.com/contacts/v1/contact/vid/{contact_id}/profile’
data = {
“properties”: [
{
“property”: property_name,
“value”: property_value
}
]
}
response = requests.post(url, headers=HEADERS, json=data)
response.raise_for_status()
print(f"Updated contact {contact_id} with {property_name}: {property_value}") # Debug statement
return response.json()
# Main function to match contacts and update sales territory
def main(event=None):
hubdb_data = get_hubdb_data()
contacts = get_contacts()
results = []
for contact in contacts:
contact_id = contact[‘vid’]
contact_properties = contact[‘properties’]
# Log all properties to understand their structure
print(f"Contact {contact_id} properties: {contact_properties}")
# Use the correct internal names for properties
contact_state = contact_properties.get(‘state’, {}).get(‘value’)
contact_country = contact_properties.get(‘ce_country_region’, {}).get(‘value’)
# Log the state and country values
print(f"Contact {contact_id} state: {contact_state}, country: {contact_country}")
if not contact_state or not contact_country:
print(f"Skipping contact {contact_id}: Missing state or country")
results.append({
‘contact_id’: contact_id,
‘status’: ‘skipped’,
‘reason’: ‘Missing state or country’
})
continue # Skip if state or country is missing
print(f"Processing contact {contact_id}: state={contact_state}, country={contact_country}")
# Find matching sales territory from HubDB table
sales_territory = None
for row in hubdb_data:
row_values = row[‘values’]
row_states = row_values.get(‘state’, [])
row_countries = row_values.get(‘country’, [])
print(f"Checking row: states={row_states}, countries={row_countries}")
if contact_state in row_states and contact_country in row_countries:
sales_territory = row_values.get(‘sales_territory’, ‘Unknown’)
print(f"Match found: sales_territory={sales_territory}")
break
if sales_territory:
update_contact_property(contact_id, ‘sales_territory’, sales_territory)
results.append({
‘contact_id’: contact_id,
‘status’: ‘updated’,
‘sales_territory’: sales_territory
})
print(f"Updated sales territory: {sales_territory} for contact: {contact_id}“)
else:
results.append({
‘contact_id’: contact_id,
‘status’: ‘no match found’
})
print(f"No match found for contact {contact_id}”)
return results
# Lambda handler function
def hubspot_handler(event, context):
return main(event)
# For local testing
if __name__ == ‘__main__’:
example_event = {}
output = main(example_event)
print(output)
