Creating e-mail templates in Odoo

In this tutorial I will teach you how to create e-mail templates in Odoo. You will learn how to create e-mail templates in XML and how to add them to the available templates. An example of the result after this tutorial:
E-mail templates example
In this tutorial I will create a new e-mail template to congratulate you on creating your own e-mail template and I will show you how to parse data from the current record in to the template with Jinja2.
Updated tutorial available on Oocademy

1. Adding the dependencies

The first thing that you need to do is add a dependency to other modules that you’ll need. Open up the __manifest.py__ file (or __openerp__.py on Odoo 8 and 9) and find the line ‘depends’. In this list you should add all the modules that you need in your custom developed module. If you want to create a new e-mail template you will need to inherit the mail module in this list. Without this inherit you won’t be able to create your own e-mail templates as Odoo wouldn’t recognize the model. In this tutorial I will create an e-mail template that can be used on the contacts form so you’ll also need the contacts module for this tutorial. Go ahead and add them both in, the result should look like this:

Now save the file and close it, it is time to create the template itself!

2. Creating the e-mail template

Now that you have the correct dependencies it is time to create the e-mail template itself. In Odoo e-mail templates are build in XML and the moment that you would send the e-mail template to somebody the Jinja2 will render your e-mail and fill it up with the corresponding data.
Create yourself a new XML file and add the default tags in the file. Since we’re creating an e-mail template we should use the noupdate=”1″ tag though. So, why do we use this tag? If you wouldn’t use it the e-mail template would be updated (and overridden) every time that you would update the module, which would result in you losing data if you’ve changed something. Your XML file should now look like this:

The next step is to create a new XML record. Just have a look at the following code and try to understand it, I will explain every line in detail under the code.

So, what exactly does this do? The very first line, which holds the record id and record model are very important. Every e-mail template that you ever want to create should be to the model ‘mail.template’. This is where all e-mail templates are saved and you should add yours there too. Now, let us go over it line by the line.

  • The ‘name’ field: This gives your e-mail template a name, which is also shown if you look at all templates under Settings > Technical > Email > Templates.
  • The ’email_from’ field: This will show the e-mail from which this e-mail is sent. This is where Odoo gets interesting! You can use ${} to get data from a record and to use this for filling in the data automatically with Jinja2. I’ll get into detail about this later on in this tutorial (see chapter 3).
  • The ‘subject’ field: This will be the title of the e-mail that is being sent to the user.
  • The ’email_to’ field: This will contain the email address to who the e-mail should be sent.
  • The ‘lang’ field: This field will make sure that the e-mail is being sent in the correct language. This field in combination with translations can be used to create multi-language e-mail templates.
  • The ‘model_id’ field: This field will tell your e-mail template on which model it will be used and where it should get data from. It always starts with ‘model_’ and should then contain the model name. It is important to replace the dots in a model name by underscores. So the model ‘res.partner’ should become ‘res_partner’.
  • The ‘auto_delete’ field: If this field is set to true it will permanently delete the e-mail after sending it, if the field is set to false the e-mail will be kept in Odoo.
  • The ‘body_html’ field: This is where you can design the content of the e-mail. You can use HTML and Jinja2 variables in this part too so you can style the template and pre-fill data before it is being sent. All the content that contains HTML or variables should be within CDATA Click here for more information about CDATA.

There, you now have a functioning e-mail template! Finally we should add some content in it. An example:

That is all, you’ve just created your very first e-mail template! Your template should now look like this:

After saving this file and installing the module you will have the e-mail template available. If you’d like to know what exactly ${} does and how you can use Jinja2 to automatically fill in data just continue reading! If you already know this or don’t want to know you can go to chapter 4.

3. Using jinja2 variables for parsing e-mail templates

Odoo e-mail templates come with jinja2 by default. This means that you can access any value on a record and fill it in on the e-mail automatically. Let us go back to the code from the e-mail template we just made. In the subject field we had the following code:

What exactly does this do? By calling ${} Jinja2 knows that this part of the XML should be parsed. By default ‘object’ will be able to access all values from the existing model, which is in this tutorial the model ‘res.partner’. If we would send out this e-mail template from a contact named ‘Administrator’ the subject would become “Congratz Administrator”:
Example rendered e-mail template

If you would want to get the contact his language for example you can access it by simply doing:

If you’d want to get the value from another model that is connected to ‘res.partner’ (many2one, many2many fields) you can also access the value too:

If you would like to go even further and create dynamic e-mail templates you can do this too. For example if you would only want to send a part of the text if the contact has a website you could do this:

In this example this part of the text would only be added to the e-mail if the contact has a website filled in on his form.
As you can see Jinja2 has quite some options and you can go a lot further. If you’d like to learn more you can find more information about Jinja2 and how it works on their website.

4. Conclusion

Creating e-mail templates in Odoo is quite easy and only requires you to write some XML. There are a lot of abilities for e-mail templates such as if/else statements, content parsing, dynamic content and so on thanks to Jinja2.
While creating e-mail templates is quite easy in Odoo it can get rather complex if you would like to use multiple if statements or dynamic content. It is always wise to test your e-mail templates in detail before placing it in production.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy




PayPal

Adding filters to existing search views

Hi guys,

In this tutorial I will teach you how to add filters to existing quick search options. In this tutorial I will add two filters to the quick search view so that you can filter  out quotations based on the total of the quotation. The one filter will only show quotations that have a higher total than 1.000, in the other filter I will only show quotations that have a total under 1.000. An example of the result after this tutorial:
Quick filters example

1. Inheriting the search view

The first thing to do is to find the search view where you want to add your own filters in. The easiest way to do this is to activate developer mode in Odoo, opening up the developer icon and by clicking on ‘Edit SearchView’:
Edit searchview

After you’ve clicked on this menuitem a dialog will pop-up where you can find all the details from the view. Now search for the field ‘External ID’ and copy the value from this field:
View id
This value is the ID to this search view, which you will need to inherit in order to add your custom filter in it later on.
In my example the ID of this view is ‘sale.sale_order_view_search_inherit_quotation’.

Now open up (or create) an XML file and inherit the existing search view like this:

What exactly does this code do? The record id will be the unique id for this view in the database and the model ‘ir.ui.view’ tells Odoo that it is a view. In the field name=”name” tag you should add an unique name for the record. The model refers to the model of the view you’re working on (in this tutorial sale.order) and the inherit_id field refers to the view from which we just copied the ‘External ID’.
Next we’ll add an arch field in which we will later on add the filter in when we want to quick filter on it.

Adding your own filters to the quick search

Alright, you’ve now inherited the view. The only thing left to do now is to add the filters on which you want to quick filter. In order to add the filters in the search you should xpath in to the search element of the view to add your own filters in. So you’ll do an xpath on the view with a position=”inside”. Inside this xpath expression you can then add the filter(s) you’d like. Your code should look like this:

That is all! When you install this module you’ll see the filters that you’ve just added show up in the quick search options. In my example it’ll look like this:
Quick filter result
After using the filter that only shows the quotations that have a total under 1.000:
Matching quotations with a total under 1.000
After using the filter that only shows the quotations that have a total over 1.000:
Matching quotations with a total over 1.000

3. Conclusion

Inheriting and expanding search views to add filters is really easy to do in Odoo and it is very powerful. You can simply inherit the view, xpath in to the search part and add all the filters that you’d like to be available by default.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy




PayPal

Adding fields to quick search views

Hi guys,

In this tutorial I will teach you how to add fields to existing quick search options. In this tutorial I will add the reference field from a quotation / order to the quick search view so that you can quick search on it. An example of the result after this tutorial:
Quick search example

1. Inheriting the search view

The first thing to do is to find the search view where you want to add your own fields in. The easiest way to do this is to activate developer mode in Odoo, opening up the developer icon and by clicking on ‘Edit SearchView’:
Edit searchview

After you’ve clicked on this menuitem a dialog will pop-up where you can find all the details from the view. Now search for the field ‘External ID’ and copy the value from this field:
View id
This value is the ID to this search view, which you will need to inherit in order to add your custom field in it later on.
In my example the ID of this view is ‘sale.sale_order_view_search_inherit_quotation’.

Now open up (or create) an XML file and inherit the existing search view like this:

What exactly does this code do? The record id will be the unique id for this view in the database and the model ‘ir.ui.view’ tells Odoo that it is a view. In the field name=”name” tag you should add an unique name for the record. The model refers to the model of the view you’re working on (in this tutorial sale.order) and the inherit_id field refers to the view from which we just copied the ‘External ID’.
Next we’ll add an arch field in which we will later on add the field we want to quick search on.

Adding your own field(s) to the quick search

Alright, you’ve now inherited the view. The only thing left to do now is to add the fields on which you want to quick search. In this example I’ll add the reference field from a quotation/order. In order to add the field in the search you should xpath in to the search element of the view to add your field in it. So you’ll do an xpath on the view with a position=”inside”. Inside this xpath expression you can then add the field(s) you’d like. Your code should look like this:

That is all! When you install this module you’ll see the field you’ve just added show up in the quick search dropdown. In my example it’ll look like this:
Quick search result

3. Conclusion

Inheriting and expanding quick search views is really easy to do in Odoo and it is very powerful. You can simply inherit the view, xpath in to the search part and add all the fields that you’d like to search on.

Do you want to try the demo code and see the source code of this tutorial? You can view it on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy




PayPal

Installing Odoo 10 (enterprise) on Ubuntu 16.04

Hi guys,

1. Introduction

In this tutorial I will learn you how to install Odoo 10 community or enterprise on Ubuntu 16.04. The script that you will use is based on the code from André Schenkels but has been updated, upgraded, improved and I’ve added Enterprise support. Do notice that if you want to install the enterprise version that you will need to be an official partner or that you need to have bought the Enterprise subscription from Odoo. Otherwise you will have no access to the Github repository for the Enterprise code!

2. Downloading the script

The first step is to download my script from Github and to add the code in a new .sh file on your Ubuntu machine, wherever you’d like this.
For example right under /home. Open up an Ubuntu terminal and cd to the directory where you’d like to keep the script and then create the file:

If you’re curious about how the whole code looks and works you can find it on my Github account.
Now open up the file and edit the parameters to your liking:

There are some things you can configure/change to your likings at the top of the script. You can choose if you wish to install Wkhtmltopdf or not, which version you’d like, where the location is and most importantly what the master admin password is.
Tip: always modify this for every Odoo you install!
If you want the enterprise version of V10 you should change the line IS_ENTERPRISE to true:

If you want the community version you can just continue and keep the IS_ENTERPRISE key on false (which is the case by default):

3. Making the Odoo installation file executable

The next step is to make this file executable. After you’ve made it executable you can execute it and everything will be installed automatically.
do this with the following command:

4.Running the script

Now that the code is in your file and the file is executable you simply have to execute it with the following command:

You will see that the script automatically starts updates, downloads required packages, creates the user, downloads the code from Github, … Eventually, if you’ve chosen to install the enterprise version, you will need to give in your Github credentials to download the Enterprise code (since this is a private repository). Fill in your details and let the script continue:
Odoo 9 Enterprise authentication
Give the script a few minutes to configure and install everything and eventually you will see something like this:
Result install script

You now have a fully functional Odoo V10 community or enterprise on your system! Congratulations.
Odoo V9 Enterprise

5. Extra information about Odoo 10 Enterprise

Since Odoo Enterprise uses code from both http://github.com/odoo/odoo and github.com/odoo/enterprise we will separate the code with this script. This will make future upgrades easier and the code is nicely separated. This means that the default V10 code will be under /odoo/odoo-server/ and all the enterprise code will be under /odoo/enterprise/.

In the script you saw there was an option to change the Odoo port (OE_PORT). When you’d change this port number to 8070 in the install script it would be applied to /etc/your-config-file.conf and this would give you the ability to change the default port.
To apply these changes you should do the following:
Changing Odoo settings
The -c will change the configuration and memorize what you’ve changed under /etc/your-config-file.conf. Because my port was set to 8070 this is telling the Odoo that it should run on port 8070. When you would now open up your browser and navigate to http://localhost:8070/ you will see it is running there:
Odoo V9 alternative port

Has this tutorial helped you, do you have any feedback or questions? Post away!
Tutorial sponsored by Oocademy




PayPal