Hello all,
We are using Odoo Online v19 with Studio, and I recently needed to figure out a way to handle multiple Invoice and Delivery addresses in Odoo Sales. To do this, I first enabled Customer Addresses following the doc linked below. Then I used Studio to add the partner_shipping_id and partner_invoice_id to the Sales module Form view. I also added {'show_address': 1} to the "Context" property of the fields to show the full address in the Form view. While this setup alone works, it allows you to select any address record, which is quite error-prone. To remedy this, I tried to use the Field->Properties->Domain to only allow addresses that had a matching parent ID of the company being invoiced/quoted, but apparently, dynamic fields don't work in the Domain.
Instead, I decided to set up a simple Automation Rule with some Python to validate these fields. Now, when an address from a different company is selected, it raises an error and prevents saving. When an address from the same company is selected, but the address type is wrong, it places a warning in the Chatter. This also takes into account a company that has no separate delivery and invoice address, as it allows the primary company record to be selected as well. I hope this helps someone out, if it does, let me know below!
To set this up, do the following:
- Create a new Automation Rule in the Sales Module
- Select "Sales Order" as the Model.
- Trigger: "On create and edit"
- Match and Apply on all records.
- Add an action and select "Execute Code"
- Paste the code from below!
# Validate Delivery Address
if record.partner_shipping_id and record.partner_id:
# Check if it belongs to the customer (blocking)
if (
record.partner_shipping_id != record.partner_id
and record.partner_shipping_id.parent_id != record.partner_id
):
raise UserError(
"❌ The Delivery Address must belong to the selected Customer. \nContact IT for help with this error."
)
# Check type (warning), only if not the base company
elif record.partner_shipping_id != record.partner_id:
if record.partner_shipping_id.type != 'delivery':
record.message_post(
body="⚠️ The selected Delivery Address is not marked as type 'Delivery'."
)
# Validate Invoice Address
if record.partner_invoice_id and record.partner_id:
# Check if it belongs to the customer (blocking)
if (
record.partner_invoice_id != record.partner_id
and record.partner_invoice_id.parent_id != record.partner_id
):
raise UserError(
"❌ The Invoice Address must belong to the selected Customer. \nContact IT for help with this error."
)
# Check type (warning), only if not the base company
elif record.partner_invoice_id != record.partner_id:
if record.partner_invoice_id.type != 'invoice':
record.message_post(
body="⚠️ The selected Invoice Address is not marked as type 'Invoice'."
)
Links:
Customer Addresses: https://www.odoo.com/documentation/19.0/applications/finance/accounting/customer_invoices/customer_addresses.html
Dynamic fields do work in the domain, but you can't enter them using Studio. You need to directly update the XML for the Studio view.