DEV Community

Cover image for How to Inherit and Make Changes to an Email Template in Odoo
Jeevachaithanyan Sivanandan
Jeevachaithanyan Sivanandan

Posted on

How to Inherit and Make Changes to an Email Template in Odoo

In Odoo, you may sometimes need to modify an existing email template for sending invoices. For instance, consider the template with the ID account.email_template_edi_invoice within the account module. To customize the content of this template, you can either create a new template or inherit the existing one and make changes. This article explains how to inherit and modify an existing email template.

Understanding the Existing Template
The email templates in Odoo are often declared within a noupdate block, which means they are marked as non-updatable to allow users to freely customize or delete them without the risk of Odoo updates overwriting their changes. Here’s a snippet of the account.email_template_edi_invoice template:

<?xml version="1.0" ?>
<odoo>
    <!-- Mail templates are declared in a NOUPDATE block
         so users can freely customize/delete them -->
    <data noupdate="1">
        <!-- Template definition -->
    </data>
</odoo>
Enter fullscreen mode Exit fullscreen mode

Since the noupdate attribute is set to 1 (True), the template is protected from updates. To make changes, we need to temporarily make this record updatable.

Steps to Modify the Email Template
Create a New XML File:

First, create a new XML file in your custom module’s data directory. For example, custom_module/data/mail_template.xml.

Add Code to Update the Template:

Add the following code to the new XML file. This code will:

Temporarily set the noupdate attribute to False.
Make the necessary changes to the email template.
Set the noupdate attribute back to True.

<?xml version="1.0"?>
<odoo>
    <data noupdate="0">
        <!-- Make the email template updatable -->
        <function name="write" model="ir.model.data">
            <function name="search" model="ir.model.data">
                <value eval="[('name', '=', 'email_template_edi_invoice'), ('module', '=', 'account')]" />
            </function>
            <value eval="{'noupdate': False}" />
        </function>

        <!-- Modify the email template -->
        <record id="account.email_template_edi_invoice" model="mail.template">
            <field name="name">Invoice: Sending</field>
            <field name="model_id" ref="account.model_account_move"/>
            <field name="email_from">{{ (object.invoice_user_id.email_formatted or object.company_id.email_formatted or user.email_formatted) }}</field>
            <field name="partner_to">{{ object.partner_id.id }}</field>
            <field name="subject">{{ object.company_id.name }} Invoice (Ref {{ object.name or 'n/a' }})</field>
            <field name="description">Sent to customers with their invoices in attachment</field>
            <field name="body_html" type="html">
                <div style="margin: 0px; padding: 0px;">
                    <!-- HTML Content of the mail template -->
                </div>
            </field>
        </record>

        <!-- Set the email template back to non-updatable -->
        <function name="write" model="ir.model.data">
            <function name="search" model="ir.model.data">
                <value eval="[('name', '=', 'email_template_edi_invoice'), ('module', '=', 'account')]" />
            </function>
            <value eval="{'noupdate': True}" />
        </function>
    </data>
</odoo>
Enter fullscreen mode Exit fullscreen mode

Update the Manifest File:

Ensure that your module’s manifest.py file includes the new XML file in the data section.

Restart Odoo:

After making these changes, restart your Odoo instance to apply the updates.
By following these steps, you can successfully inherit and modify existing email templates in Odoo. This method ensures that your changes are preserved and not overwritten by future updates. Remember to verify your changes by sending out an invoice and checking that the updated template is used.

Top comments (0)