Draft an ERPNext sales order when a HubSpot deal is won

By General Input

When a deal is marked closed won in HubSpot, the matching order is drafted in ERPNext with the right customer attached, ready for finance to review.

Integrations

  • ERPNext
  • HubSpot
  • Slack Bot

Type

Agentic Task

Categories

  • Sales
  • Operations

When a HubSpot deal moves to closed won, I want the matching sales order created in ERPNext automatically so my sales team stops rekeying it by hand.

Trigger this on a HubSpot webhook for deal stage changes. The webhook fires on every stage change, so the first thing to do is check the new stage: if the deal has not just moved into the closed won stage, stop and do nothing. Do not create anything for deals moving between other stages.

Once you confirm the deal is closed won, use HubSpot Get Deal on the deal id from the webhook to pull the deal name, the amount, the close date, and the associated company and contact. Request the associations so you get the linked company and contact ids back, then use HubSpot Get Company for the company name, domain, tax id if it is stored, and billing address, and HubSpot Get Contact for the primary contact's name, email, and phone. Treat the company as the account and the contact as the person to put on the order.

Now find the customer in ERPNext before you create anything. Use ERPNext List Documents on the Customer DocType, requesting fields such as name, customer_name, and tax_id, and search for an existing customer that matches the HubSpot company. Match on the company name and on the tax id when HubSpot has one. Be sensible about near matches: ignore case, punctuation, and trailing suffixes like Ltd, Limited, Inc, LLC, and GmbH, so that Acme Ltd and ACME Limited are understood as the same account. A matching tax id is strong evidence and should win over a fuzzy name difference.

Only create a new customer with ERPNext Create Document on the Customer DocType when there is genuinely no confident match. This is important: duplicate customer accounts are worse than a missing one, so when you are unsure, prefer the existing customer and say so in the Slack message rather than creating a second account. When you do create a customer, set customer_name from the HubSpot company name and fill in the tax id and territory where you have them.

Next, work out the order lines. Use ERPNext List Documents on the Item DocType to look up items, matching on item_code and item_name against the product wording in the deal, for example the deal name or the product referenced on the deal. Note that the detailed HubSpot line item breakdown is not available to you here, so build the order from the deal amount and the item you can identify. When you can confidently match a single ERPNext item, use it as the order line with the deal amount as the rate. When you cannot confidently match an item, fall back to the generic or default item the user has nominated, put the deal amount on it, and record clearly that this line was not confidently mapped so a human can correct it.

Then create the Sales Order in ERPNext with Create Document on the Sales Order DocType. Set the customer to the ERPNext customer you matched or created, set transaction_date and delivery_date from the HubSpot close date, set the company, and add the item rows with item_code, qty, and rate. Leave the Sales Order as a draft. Do not submit it, and do not set docstatus to anything other than 0. Submitted ERPNext documents cannot be edited, and the entire point of this workflow is that finance reviews the pricing before the order is approved.

Finally, post a message to the sales ops Slack channel with Slack Bot Send a Message. Include the HubSpot deal name and amount, the new ERPNext Sales Order id, and whether you reused an existing customer or created a new one and why. Then list, clearly and separately, any line that you could not confidently map to an ERPNext item, so a human can fix it before the order is submitted. If nothing needed attention, say that plainly so the team knows the order is clean. Keep the message short and scannable, and use Slack formatting with single asterisks for bold.

If anything fails along the way, for example the deal has no associated company or ERPNext rejects the order, do not leave it silent: post what happened to the same Slack channel with the deal name and the reason, so the order can be raised manually.

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