Automated Invoice Generation from CRM to Xero (n8n)

Streamline your Quote-to-Cash process using n8n's visual workflow engine and self-hosted privacy.

Tools: CRMXero

Platform: n8n

Short Answer

A fully automated pipeline where a closed deal in your CRM instantly verifies or creates a contact in Xero, generates a drafted sales invoice with line items, and notifies the team of success—all while maintaining a local execution log in n8n.

The Problem

Manual data entry between CRM systems and accounting software leads to invoicing delays, human error in tax calculations, and fragmented customer data. Business owners often lose hours re-typing 'Won' deal information into Xero, delaying cash flow.

The Outcome

A fully automated pipeline where a closed deal in your CRM instantly verifies or creates a contact in Xero, generates a drafted sales invoice with line items, and notifies the team of success—all while maintaining a local execution log in n8n.

Step-by-Step Guide

1. **Setup CRM Trigger**: Use the 'Webhook' node (for real-time) or the 'CRM' specific node (e.g., HubSpot, Pipedrive) to watch for 'Deal Updated' or 'Deal Won' events. 2. **Configure Xero Credentials**: In n8n, go to Credentials > New. Select 'Xero OAuth2 API'. Follow the OAuth flow to authorize n8n to access your Xero organization. 3. **Search for Existing Contact**: Add a 'Xero' node. Set Resource to 'Contact' and Operation to 'Get All'. Use a Filter parameter to search by the CRM's email: `{{ $json.email }}`. 4. **Logic Branching (IF Node)**: Add an 'IF' node to check if the previous step returned a Contact ID. Expression: `{{ $node["Xero"].json["ContactID"] }}` is not empty. 5. **Create/Update Contact**: Based on the IF node, add another Xero node. If false, use 'Create Contact'. If true, use 'Update Contact' to ensure the billing address is current. 6. **Format Line Items**: Use a 'Set' node or 'Code' node (JavaScript) to map CRM products into Xero's array format. Xero requires an array of objects containing `Description`, `Quantity`, `UnitAmount`, and `AccountCode`. 7. **Generate Invoice**: Add a Xero node. Set Resource to 'Invoice' and Operation to 'Create'. Map the Contact ID from Step 5 and the Line Items from Step 6. Set 'Status' to `DRAFT` for safety or `AUTHORISED` for automation. 8. **Data Transformation with Expressions**: Use n8n expressions like `{{ $now.format('YYYY-MM-DD') }}` for the invoice date and `{{ $json.total.toNumber() }}` to ensure numeric values. 9. **Error Handling**: Create a separate 'Error Trigger' workflow or use the 'On Error' settings on individual nodes to send a message to Slack or Email if an invoice fails (e.g., due to a missing Tax ID). 10. **Activate Workflow**: Save the workflow and toggle the 'Active' switch in the top right of the n8n UI.