Enable Dark Mode!
how-to-inherit-and-update-an-existing-mail-template-in-odoo-16.jpg
By: Swapna

How to Inherit & Update an Existing Mail Template in Odoo 16

Technical Odoo 16

In this article, let us look at how to inherit and edit an existing mail template in Odoo 16. We must do this in order to make any adjustments or updates to the content of an existing template. Let’s consider an example of updating the mail template in the accounting module.

How to Inherit & Update an Existing Mail Template in Odoo 16-cybrosys

Upon clicking the “SEND & PRINT” button from the “account.move” model, the above wizard will be open. Here, we are going to make changes. From the wizard, it is clear that the used template name is “Invoice: Sending”. Let’s search for the code.

<?xml version="1.0" ?>
<odoo>
    <!-- Mail template are declared in a NOUPDATE block
         so users can freely customize/delete them -->
    <data noupdate="1">
        <!--Email template -->
        <record id="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;">
    <p style="margin: 0px; padding: 0px; font-size: 13px;">
        Dear
        <t t-if="object.partner_id.parent_id">
            <t t-out="object.partner_id.name or ''">Brandon Freeman</t> (<t t-out="object.partner_id.parent_id.name or ''">Azure Interior</t>),
        </t>
        <t t-else="">
            <t t-out="object.partner_id.name or ''">Brandon Freeman</t>,
        </t>
        <br /><br />
        Here is your
        <t t-if="object.name">
            invoice <span style="font-weight:bold;" t-out="object.name or ''">INV/2021/05/0005</span>
        </t>
        <t t-else="">
            invoice
        </t>
        <t t-if="object.invoice_origin">
            (with reference: <t t-out="object.invoice_origin or ''">SUB003</t>)
        </t>
        amounting in <span style="font-weight:bold;" t-out="format_amount(object.amount_total, object.currency_id) or ''">$ 143,750.00</span>
        from <t t-out="object.company_id.name or ''">YourCompany</t>.
        <t t-if="object.payment_state in ('paid', 'in_payment')">
            This invoice is already paid.
        </t>
        <t t-else="">
            Please remit payment at your earliest convenience.
            <t t-if="object.payment_reference">
                <br /><br />
                Please use the following communication for your payment: <span style="font-weight:bold;" t-out="object.payment_reference or ''">INV/2021/05/0005</span>.
            </t>
        </t>
        <br /><br />
        Do not hesitate to contact us if you have any questions.
        <t t-if="not is_html_empty(object.invoice_user_id.signature)">
            <br /><br />
            <t t-out="object.invoice_user_id.signature or ''">--<br/>Mitchell Admin</t>
        </t>
    </p>
</div>
            </field>
            <field name="report_template" ref="account_invoices"/>
            <field name="report_name">Invoice_{{ (object.name or '').replace('/','_') }}{{ object.state == 'draft' and '_draft' or '' }}</field>
            <field name="lang">{{ object.partner_id.lang }}</field>
            <field name="auto_delete" eval="True"/>
        </record>
        <record id="mail_template_data_payment_receipt" model="mail.template">
            <field name="name">Payment: Payment Receipt</field>
            <field name="model_id" ref="account.model_account_payment"/>
            <field name="subject">{{ object.company_id.name }} Payment Receipt (Ref {{ object.name or 'n/a' }})</field>
            <field name="partner_to">{{ object.partner_id.id }}</field>
            <field name="description">Sent manually to customer when clicking on 'Send receipt by email' in payment action</field>
            <field name="body_html" type="html">
<div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px; font-size: 13px;">
        Dear <t t-out="object.partner_id.name or ''">Azure Interior</t><br/><br/>
        Thank you for your payment.
        Here is your payment receipt <span style="font-weight:bold;" t-out="(object.name or '').replace('/','-') or ''">BNK1-2021-05-0002</span> amounting
        to <span style="font-weight:bold;" t-out="format_amount(object.amount, object.currency_id) or ''">$ 10.00</span> from <t t-out="object.company_id.name or ''">YourCompany</t>.
        <br/><br/>
        Do not hesitate to contact us if you have any questions.
        <br/><br/>
        Best regards,
        <t t-if="not is_html_empty(user.signature)">
            <br/><br/>
            <t t-out="user.signature or ''">--<br/>Mitchell Admin</t>
        </t>
    </p>
</div>
</field>
            <field name="report_template" ref="account.action_report_payment_receipt"/>
            <field name="report_name">{{ (object.name or '').replace('/','-') }}</field>
            <field name="lang">{{ object.partner_id.lang }}</field>
            <field name="auto_delete" eval="True"/>
        </record>
        <!-- Credit note template -->
        <record id="email_template_edi_credit_note" model="mail.template">
            <field name="name">Credit Note: 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 }} Credit Note (Ref {{ object.name or 'n/a' }})</field>
            <field name="description">Sent to customers with the credit note in attachment</field>
            <field name="body_html" type="html">
<div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px; font-size: 13px;">
        Dear
        <t t-if="object.partner_id.parent_id">
            <t t-out="object.partner_id.name or ''">Brandon Freeman</t> (<t t-out="object.partner_id.parent_id.name or ''">Azure Interior</t>),
        </t>
        <t t-else="">
            <t t-out="object.partner_id.name or ''">Brandon Freeman</t>,
        </t>
        <br /><br />
        Here is your
        <t t-if="object.name">
            credit note <span style="font-weight:bold;" t-out="object.name or ''">RINV/2021/05/0001</span>
        </t>
        <t t-else="">
            credit note
        </t>
        <t t-if="object.invoice_origin">
            (with reference: <t t-out="object.invoice_origin or ''">SUB003</t>)
        </t>
        amounting in <span style="font-weight:bold;" t-out="format_amount(object.amount_total, object.currency_id) or ''">$ 143,750.00</span>
        from <t t-out="object.company_id.name or ''">YourCompany</t>.
        <br /><br />
        Do not hesitate to contact us if you have any questions.
        <t t-if="not is_html_empty(object.invoice_user_id.signature)">
            <br /><br />
            <t t-out="object.invoice_user_id.signature or ''">--<br/>Mitchell Admin</t>
        </t>
    </p>
</div>
            </field>
            <field name="report_template" ref="account_invoices"/>
            <field name="report_name">Credit_note_{{ (object.name or '').replace('/','_') }}{{ object.state == 'draft' and '_draft' or '' }}</field>
            <field name="lang">{{ object.partner_id.lang }}</field>
            <field name="auto_delete" eval="True"/>
        </record>
    </data>
</odoo>

This is the existing code. Here, the record is non updatable due to the presence of the keyword noupdate = “1”. So for making modifications, we need to change it as updatable. For that, we can use function and value tags.

<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>

Here “email_template_edi_invoice” is the existing record id. First, we are searching this email template and writing the no_update value as False. This will help us to make changes in the email template.

We may now make the necessary changes to the email template. For example, I'm including the Invoice Date in the email's subject and the report's name that will be attached to it. We must edit the mail template record's topic and report_name fields to do so. Because we will be updating them from the custom module, provide the module name along with the record id. The mail template in our case is located in the "account" module. As a result, we must specify the id "account.email_template_edi_invoice".

<record id="account.email_template_edi_invoice" model="mail.template">
    <field name="subject">{{ object.company_id.name }} Invoice (Ref {{ object.name or 'n/a' }}) {{ object.invoice_date }}</field>
    <field name="report_name">Invoice_{{ (object.name or '').replace('/', '_') }}{{ object.state == 'draft' and '_draft' or '' }}_{{ object.invoice_date.strftime('%Y_%m_%d') }}</field>
</record>
After we make the adjustments, we must return the noupdate to True for the record.
<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>

We're doing the same thing we did from the beginning. However, we will use the search and write functions to find the mail template record and then set the 'noupdate' to True. It will help to avoid making unnecessary changes to the mail template. This code must be placed in an XML file in the custom module's data directory. We must specify the file in the data parameter and the module of the existing template (in this example, "account" because we are upgrading the "email_template_edi_invoice template") in the manifest file's "depends" list.

We may inherit and update existing email templates in Odoo and other custom modules in this way. This is how to inherit and edit an existing mail template in Odoo 16.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message