# Draft an ERPNext sales order when a HubSpot deal is won

> 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.

- Workflow type: agent
- Services: ERPNext, HubSpot, Slack Bot
- Categories: Sales, Operations
- Published: 2026-08-04

## What it does

- Watches HubSpot for deals that move into your closed won stage and picks up the deal value, close date, and the company and contact attached to it
- Looks for the customer in ERPNext first, matching on company name and tax id, and only opens a new customer record when there is genuinely no match, so your account list stays clean
- Drafts the sales order in ERPNext against that customer and deliberately leaves it as a draft, so nobody can bill a customer before finance has checked the pricing
- Posts a note to your sales ops channel with the deal name, the new order number, and anything it was not confident about, so a person can fix it before the order is approved

## What you'll need

- A HubSpot account where your sales team works deals through a closed won stage
- An ERPNext site, plus a login that is allowed to view and create customers, items, and sales orders
- A Slack workspace and a channel where your sales ops or finance team keeps an eye on new orders
- A default item in ERPNext to fall back on when a deal cannot be matched to a specific product

## Prompt

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.

## How to customize

- Change which deal stage starts the workflow, for example a Ready to Invoice stage instead of closed won
- Point the alert at a different Slack channel, or send it to finance instead of sales ops
- Loosen or tighten how confident the match on an existing customer has to be before a new one is created
- Swap the fallback item, or have the workflow skip small deals under a value you set

## FAQ

### Will this create duplicate customers in ERPNext?

That is the main thing it is built to avoid. Before creating anything, it searches your existing ERPNext customers and compares the company name and tax id from HubSpot against them. It only creates a new customer when there is no convincing match, and it tells you in Slack which route it took so you can spot check it.

### Does the order get approved and sent to the customer automatically?

No. The order is deliberately left as a draft. In ERPNext, once an order is submitted it can no longer be edited, and the whole point here is that your finance team gets to review the pricing first. Someone has to open it and approve it by hand.

### What happens if the workflow cannot tell which product the deal is for?

It still drafts the order, but it uses the fallback item you nominated and says clearly in the Slack message that the line needs a human to set the right product. Nothing is quietly guessed and hidden from you.

### Will this work on a free HubSpot plan?

It works on any HubSpot plan where your team tracks deals through pipeline stages. You will need stage change notifications turned on for the pipeline you want to watch, which is a setting in HubSpot rather than something you build.

### What if a deal is reopened or moves back out of closed won?

The workflow only acts when a deal moves into your won stage, so moving a deal backwards does not trigger anything. Because the order is still a draft, cancelling or amending it in ERPNext is straightforward.

Use this prompt in General Input: https://www.generalinput.com/prompts/draft-an-erpnext-sales-order-when-a-hubspot-deal-is-won