4

I want to write a custom module to replace mail templates.

Those templates are included in base Odoo addons, such as sale:

The sale.order template ìs provided by the file /sale/data/mail_template_data.xml

This template is as follows:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
    <!--Email template -->
    <record id="email_template_edi_sale" model="mail.template">
        <field name="name">Sales Order - Send by Email</field>¬

    ...
</odoo>

As the custom module wants to replace this standard base template:

  • Can a record with the same id be provided by the custom module to replace this mail template?
  • What shall be writte in <data noupdate>?
  • What will happen if module sale is updated?

Odoo 10 community edition.

Naglis
  • 2,583
  • 1
  • 19
  • 24
M.E.
  • 4,955
  • 4
  • 49
  • 128

3 Answers3

4

For replacing the Email Templates just add the addon name in-front of the template name followed by dot(.) and make sure that you delete the default email template from the front end. Then update your custom addon. This will replace the old template.

Example:

<record id="sale.email_template_edi_sale" model="mail.template">
    <field name="name">Sales Quotation</field>
    <field name="email_from">${(object.user_id.email and '%s &lt;%s&gt;' % (object.user_id.name, object.user_id.email) or '')|safe}</field>
    <field name="subject">${object.company_id.name} ${object.state in ('draft', 'sent') and 'Quotation' or 'Order'} (Ref ${object.name or 'n/a' })</field>
    <field name="partner_to">${object.partner_invoice_id.id}</field>
    ....
    ....
</record>
Naglis
  • 2,583
  • 1
  • 19
  • 24
sfx
  • 1,793
  • 17
  • 24
  • 1
    Is there any way to remove the manual step of deleting the old template from the front end? – M.E. Feb 19 '18 at 06:15
  • Activate the Debug Mode then go to Settings-->Technical Settings-->Email-->Template, search for the sale order template and delete it. – sfx Feb 19 '18 at 06:53
  • I know that, but I would like to know if it is possible to do it via custom module so I do not have to manually remove all old mail templates in each installation. So instead of adding a new template just replacing the old one with the new one installed in the custom module. – M.E. Feb 19 '18 at 07:04
  • Look for examples with `` tags in Odoo code. – CZoellner Feb 19 '18 at 08:30
0
<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">
    <!--Email template -->
    <record id="email_template_edi_sale" model="mail.template">
        <field name="name">Sales Order - Send by Email</field>

    ...
</odoo>

Please add your code to

    <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data>
        <delete model="mail.template" search=" 
[('id','=',ref('sale.email_template_edi_sale'))]"/>
        
         <!--Email template -->
        <record id="sale.email_template_edi_sale" model="mail.template">
            <field name="name">Sales Order - Send by Email</field>
    
        ...
    </odoo>

This will delete the original mail template and add a new template with the same ID so that the odoo functionlity is not disturbed.

  • This won't work. First of all you can't put code before `` And this won't add new template with the exactly the same ID - instead of `sale.email_template_edi_sale` there will be `custom_module.email_template_edi_sale` – AnJ Mar 03 '21 at 08:45
-1

Don't delete original template, you will lose the original module field and somethings will stop to work. Instead change noupdate value in ir.model.data for your template.

To do this automatically on module update:

  • Modify model 'ir.model.data' and add an allow_update method, creating a ir_model_data.py in models folder (modify __init__.py to include new file):

      from odoo import models, fields, api 
      class IrModelData(models.Model):
          _inherit = 'ir.model.data'
    
          @api.model
          def allow_update(self, module, name, model):
              self.search([('module',  '=', module), ('name', '=', name), ('model', '=', model)])[0].noupdate = False
    
  • Add function call element to allow_update before your record update and pass the original module name, external_id and 'mail.template':

      <?xml version="1.0" encoding="utf-8"?>
      <odoo>
        <data noupdate="0">
          <function model='ir.model.data' name='allow_update'>
            <value>sale</value>
            <value>email_template_edi_sale</value>
            <value>mail.template</value>
          </function>
          <record id="sale.email_template_edi_sale" model="mail.template" >
            <field name="body_html" type="html">
              <div style="margin: 0px; padding: 0px;">
                <p style="margin: 0px; padding: 0px; font-size: 13px;">
                    % set doc_name = 'quotation' if object.state in ('draft', 'sent') else 'order'
                    Dear ${object.partner_id.name}
    
TlmaK0
  • 3,578
  • 2
  • 31
  • 51