Auto-route new HubSpot contacts to the right sales territory

By General Input

Every new HubSpot contact gets geocoded, assigned to the closest sales territory, and announced in the right Slack channel within seconds.

Integrations

  • HubSpot
  • Geolocation
  • Slack

Type

Deterministic Code

Categories

  • Sales
  • Operations

Build me a code workflow that auto-routes new HubSpot contacts to a sales territory and announces them in Slack.

Trigger: a HubSpot outgoing webhook that fires whenever a new contact is created. Subscribe to contact.creation events and pull the contact id out of the webhook payload.

Step 1. Fetch the contact from HubSpot using the id from the webhook. Read at minimum: firstname, lastname, email, company, address (street), city, state, zip, and the HubSpot record URL for that contact.

Step 2. Geocode the address with the Geolocation integration's Forward Geocode operation. Concatenate street, city, state, and zip into a single query string.

Step 3. If geocoding fails, returns no result, or returns an ambiguous low-confidence match, set territory to "unassigned", skip the distance step, and continue straight to step 6 using the review channel.

Step 4. Pick the closest territory using the Geolocation integration's Calculate Distance Matrix operation. The geocoded contact location is the single origin; the destinations are a configurable list of territory HQs declared at the top of the workflow. Default the list to:

- Northeast HQ: New York, NY -> #sales-northeast

- Southeast HQ: Atlanta, GA -> #sales-southeast

- Central HQ: Chicago, IL -> #sales-central

- West HQ: San Francisco, CA -> #sales-west

Pick the territory with the shortest driving distance. Keep both the chosen territory name and the miles from its HQ for the Slack message.

Step 5. Update the HubSpot contact using the HubSpot Update Contact operation. Write three custom properties: hs_latitude (the geocoded latitude), hs_longitude (the geocoded longitude), and territory (the chosen territory name, or "unassigned" on geocoding failure). Add a short note in the workflow that these three custom contact properties must be created in HubSpot before the workflow runs.

Step 6. Post a Slack message with the Slack integration's Send a Message operation. On a successful match, post to the matched territory's channel. On failure or unassigned, post to a configurable review channel that defaults to #leads-needs-review. The message should be short, one or two lines plus the link, and include: contact full name, company, geocoded address, miles from the territory HQ (rounded), and a link to the contact in HubSpot.

Make the following easy to edit at the top of the workflow: the HubSpot account, the Slack workspace, the territory-to-channel map, and the review channel name. Keep the flow deterministic and linear: webhook -> fetch contact -> geocode -> distance matrix -> update HubSpot -> post to Slack.

Related prompts

Explore more prompts
Call overdue Xero customers with an AI collections agentWin back LiveChat visitors whose chats went unansweredTurn Mailjet email clicks into ranked HubSpot follow-upsiMessage campaign console with pre-flight checks and delivery boardChat quality review board for LiveChat support leadsA searchable RFP answer library your bid team drafts fromSee which target accounts just started advertising on LinkedInAccount health board that puts product usage next to your CRMLook up a customer's full chat history mid conversationLusha prospecting workbench with credit-safe reveals