Skip to Content

Sales Order Automations (Part Four): Automate Invoice Creation

Recap of the Series

We've built a powerful, end-to-end sales workflow in Odoo.

  • Part 1: Automatically confirmed sales orders for VIP clients. Click here.
  • Part 2: Sent automated email notifications to the sales team. Click here.
  • Part 3: Triggered stock reservation and validation upon order confirmation. Click here.

Now, we will complete the business process by setting up an action to automatically create and prepare an invoice for the customer as soon as the delivery is marked as done. This is a critical step for improving your billing cycle and cash flow.


Step 1: Create a New Automated Action

Unlike the previous parts, this automation is triggered by the completion of a delivery, not a sales order. Therefore, the action's model will be different.

  • Go to Settings > Technical > Automated Actions.
  • Click Create.


Step 2: Define Basic Settings

  • Name: Automate Invoice Creation on Delivery
  • Model: Delivery Order (stock.picking)
  • Trigger: On Update
  • Apply On: Deliveries where the state changes to Done. To ensure this only applies to sales orders, use the following domain filter: [('state', '=', 'done'), ('sale_id', '!=', False)]
  • Action To Do: Execute Python Code


Step 3: Add Python Code

This script will find the original sales order related to the delivery and then call the Odoo function to create the invoice.

Python

if record.sale_id and record.sale_id.invoice_status == 'to invoice':
    record.sale_id._create_invoices()
  • record.sale_id: This finds the sales order linked to the delivery (stock.picking) record that triggered the action.
  • invoice_status == 'to invoice': This condition ensures an invoice is only created if the sales order is ready for billing.
  • _create_invoices(): This is the official Odoo method that creates a draft invoice and links it back to the sales order.


Step 4: Save and Test the Automation

  • Save the automated action.
  • Create a new sales order, confirm it, and validate the delivery (the Done button on the delivery form).
  • Go back to the sales order. You should now see a smart button for "Invoices" with a count of 1. Click it to see the newly created draft invoice.


Conclusion

With this step, your sales workflow is almost fully automated. The system now handles everything from confirmation to stock reservation and seamless invoicing. Your accounting team can simply go in, review the draft invoices, and send them.

In our final part, we will close the loop by learning how to automatically create a follow-up activity for the salesperson after the invoice is sent.


Sales Order Automations (Part Three): Automate Stock Reservation