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.
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.
Additional information
What does this prompt do?
- 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 do I need to use this?
- 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
How can I customize it?
- 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
FAQs
Will this create duplicate customers in ERPNext?
Does the order get approved and sent to the customer automatically?
What happens if the workflow cannot tell which product the deal is for?
Will this work on a free HubSpot plan?
What if a deal is reopened or moves back out of closed won?
Related templates
Every weekday, find every unpaid JobNimbus invoice, email each customer one reminder that gets firmer as it ages, and post a receivables summary to Slack.
Every weekday at 7am, your active courses are checked for missing work, silent logins, and slipping grades, with a ranked list sent to your advising channel.
Every hour, find the tickets closest to breaching, leave a nudge on each one, and post a ranked at-risk list to your support channel.
Every weekday afternoon, each student who is behind gets a warm, personal message in their Canvas inbox listing exactly what they owe.
Every Monday at 8am, see exactly which assignments have work waiting, sorted worst first, posted to Slack and logged to a spreadsheet.
Every weekday morning we compare your password vault against your HR records and suspend access for anyone who has already left.
Stop rekeying won deals into your ERP.
Every closed won deal lands in ERPNext as a reviewed-by-a-human draft order, without your sales team typing it twice.