Route new HubSpot leads to the nearest rep with a drive-time map

By General Input

When a new contact lands in HubSpot, pick the rep with the shortest live drive to the lead, update the record, and post a Slack alert with the ETA and a map.

Integrations

  • HubSpot
  • Mapbox
  • Slack Bot

Type

Agentic Task

Categories

  • Sales

Build me an agent workflow that runs every time a new contact is created in HubSpot (contact.creation). The goal is to auto-assign the closest sales rep to the lead based on real drive time, write the decision back to HubSpot, and announce it in Slack with a map.

For each new HubSpot contact, the agent should:

1. Read the contact's mailing address fields (address, city, state, postal code, country). If any of those are missing so the address cannot be formed cleanly, skip this contact and do not post anything to Slack. Log the skip with a short reason.

2. Call Mapbox Forward Geocode (v6) to turn the address into a longitude/latitude. Use the structured-input form when all address parts are present, otherwise fall back to a free-form query. If the top result has low relevance or geocoding fails outright, skip the contact and log why.

3. Compare the lead against a configurable roster of sales reps. The roster lives at the top of the workflow as a list of { name, slackUserId, homeLng, homeLat } so I can edit it without changing the logic. Call Mapbox Get Distance Matrix with profile=mapbox/driving-traffic, the lead as the single origin (sources=0), and every rep as a destination. Pick the rep with the shortest duration. Convert that duration to whole minutes. Keep the roster size at or under 24 destinations per call (Mapbox driving-traffic matrix cap is 10 coordinates per request on the free tier and 25 on enterprise; the workflow should chunk if needed).

4. Update the HubSpot contact via Update Contact. Write the latitude and longitude, the assigned rep's name, and the drive-time minutes into custom contact properties. The exact property names should be configurable at the top of the workflow (defaults: lead_latitude, lead_longitude, assigned_rep, drive_time_minutes). By default, only set assigned_rep if it is currently empty so manual overrides stick.

5. Render a Mapbox Get Static Map Image centered on the lead's coordinates with a single pin marker at the lead, zoom around 11, and a clean style (default to mapbox/streets-v12). Size 600x400 PNG is fine. Save the image bytes.

6. Post a Slack message to the configured sales channel using Slack Bot Send a Message. The message should include: the lead's name and company, a link to the HubSpot contact record, an @-mention of the assigned rep via their Slack user ID, the drive time in minutes, and the static map image (upload via Slack Bot Upload a File and reference it in the message, or post the message first and reply in-thread with the image, whichever the platform supports cleanly).

Configurable inputs that should be exposed at the top of the workflow: the HubSpot custom property names (lead_latitude, lead_longitude, assigned_rep, drive_time_minutes), the Slack sales channel ID, the rep roster array, the Mapbox style and zoom for the static image, the geocoding relevance threshold (default 0.8), and a boolean for whether to overwrite an existing assigned_rep value.

Error handling: if any step fails after a successful geocode (matrix, HubSpot update, map render, Slack post), log the error with the contact ID and continue. Do not retry endlessly. Never post a malformed Slack alert; if the image fails, post the text alert without it.

Related prompts

Explore more prompts
A brand asset library your marketing team actually searchesTurn Mailjet email clicks into ranked HubSpot follow-upsClean out the Looker dashboards and Looks nobody opensLiveKit live operations console for room moderationWake up dormant Keap leads with a researched reasoniMessage campaign console with pre-flight checks and delivery boardLiveChat coverage board for planning next week's shiftsPhone routing control panel for LiveKit voice agentsA searchable RFP answer library your bid team drafts fromLinkedIn Ads budget pacing dashboard for every client account