Enable Dark Mode!
inheriting-existing-mail-templates-in-odoo.jpg
By: Risha Ct

Inheriting Existing Mail Templates in Odoo

Technical

In this blog, we are going to discuss how to inherit an existing mail template in odoo. This will be needed in cases where we need to add some modifications to the content of a template that already exists. The mail templates are added as data with no update set as true. So in order to add changes to them, we need to make the record updatable first. For this, let us use the function tag to set the noupdate as false, then make the necessary changes to the template, and then set the noupdate back to true. Let us try to modify the send-by-mail template in sales quotation for this example.
inheriting-existing-mail-templates-in-odoo-cybrosys
Let's say we need to add the order creation date to the subject of this mail. So we need to add a data file and first remove the noupdate of this template by using the function tag.
<function name = "write" model = "ir.model.data">
   <function name = "search" model = "ir.model.data">
       <value eval = "[('name', '=', ' mail_template_sale_confirmation '),
        (' module ',' = ',' sale ')] "/>
   </function>
   <value eval =" {' noupdate ': False} "/>
</function>
Here 'mail_template_sale_confirmation' is the record id of the template that we need to modify and the data file of this template is in the 'sale' module, so we search for the data providing the name and module in the value part of the search in the function tag. Then the record found using the search is updated by setting 'noupdate' as 'False'. This allows us to make changes to the record. So now we can add the date to the subject of the template. 
<record id = "sale.mail_template_sale_confirmation" model = "mail.template">
   <field name = "subject"> $ {object.company_id.name} $ {(object.get_portal_last_transaction (). state == 'pending') and 'Pending Order' or 'Order'} (Ref $ {object.name or 'n / a'}) $ {object.date_order} </field>
</record>
Here we have specified the id as sale.mail_template_sale_confirmation and model as 'ir.template' inorder to override the existing template record and then modify the subject of the mail. In the subject portion, we have added the order date field at the end as $ {object.date_order}. With this, the subject of the template gets updated. Now that our changes are completed, we need to set the record back to noupdate. For this, we can use the function tag again.
<function name = "write" model = "ir.model.data">
   <function name = "search" model = "ir.model.data">
       <value eval = "[('name', '=', ' mail_template_sale_confirmation '),
        (' module ',' = ',' sale ')] "/>
   </function>
   <value eval =" {' noupdate ': True} "/>
</function>
Here, we have searched for the sale template and set the 'noupdate' as True the way it was before editing. We need to make this update in the subject of the existing sale template only once, so we can set our data file as noupdate = "1". 
<? xml version = "1.0"?>
<odoo>
   <data noupdate = "1">
       <function name = "write" model = "ir.model.data">
           <function name = "search" model = "ir. model.data ">
               <value eval =" [('name', '=', 'mail_template_sale_confirmation'), ('module', '=', 'sale')] "/>
           </function>
           <value eval = "{'noupdate': False}" />
       </function>
       <record id = "sale.mail_template_sale_confirmation" model = "mail.template">
           <field name = "subject"> $ {object.company_id.name} $ { (object.get_portal_last_transaction (). state == 'pending') and 'Pending Order' or 'Order'} (Ref $ {object.name or 'n / a'}) $ {object.date_order} </field>
       </record>
      
       <function name = "write" model = "ir.model.data">
           <function name = "search" model = "ir.model.data">
               <value eval = "[('name', '=', ' mail_template_sale_confirmation '), (' module ',' = ',' sale ')] "/>
           </function>
           <value eval =" {' noupdate ': True} "/>
       </function>      
   </data>
< odoo>
Now, when we go to the sales quotation and click on send by email, it will have the date in the subject like this.
inheriting-existing-mail-templates-in-odoo-cybrosys
We need to add this file to the data folder of the custom module and specify it in the data part of the manifest file. Also, specify the module of the existing template ('sale' in this example as we are updating the subject of 'mail_template_sale_confirmation' template) in the 'depends' part of the manifest. By using this method, we can make changes to the existing email templates in odoo.


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



3
Comments

Phat Dang

I'll try this, and not working.

29/07/2021

-

4:36AM

Maher

I tried it in odoo 14 but does not work

24/05/2021

-

2:39PM

Fernanda

Tried this in Odoo 13 but its not working

02/11/2021

-

7:14AM



Leave a comment



whatsapp_icon
location

Calicut

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

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