Odoo, an open-source business application suite, provides a powerful framework for customizing and extending its functionality. One common customization is invoking functions from XML files, allowing developers to execute specific actions within their Odoo modules. In this blog post, we'll explore the process of invoking functions from XML files in Odoo 17.
There are two ways to make XML trigger a function:
1. Calling a function without any arguments.
2. Calling a function with arguments.
In this blog, I'll show you how to use XML to make a new partner by using a special method.
1. Calling a function without any arguments
In this way, you can call a function within a model without sending any parameters. The XML syntax for doing this is provided below.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<function model="res.partner" name="_create_new_partner"/>
</data>
</odoo>
Within the <data> tag, you'll find a <function> element. This element signifies a function call within Odoo. Specifically, it references a function named _create_new_partner in this instance. The model attribute designates the Odoo model where the function will take effect, which, in this case, is res.partner.
Upon executing this function, it will carry out a particular action tied to the creation of a new partner or contact record in the Odoo database.
Now, let's delve into the Python file to examine how this function is written.
class ResPartner(models.Model):
_inherit = "res.partner"
@api.model
def _create_new_partner(self):
self.create(dict(name=New partner'))
2. Calling a function with arguments
We can activate a function within a model by providing parameters. The XML syntax for this is as follows:
<data>
<function model="res.partner" name="func_with_params">
<value>John</value>
</function>
</data>
Inside the "function" element, there's a "value" tag containing the text "John." This specific value is being passed as an argument to the func_with_params function. Now, let's delve into the Python file to examine how this function is written.
class ResPartner(models.Model):
_inherit = "res.partner"
@api.model
def func_with_params(self, name):
self.create(dict(name='New partner'))
The func_with_params method demands a parameter, and a single name must be provided. By utilizing the create method, new entries are appended to the res.partner table. The name supplied as an argument is then utilized to generate the record.
In this blog post, we've explored the process of invoking functions from XML files in Odoo 17. Whether you're calling functions without parameters or passing specific details, understanding this customization technique enhances the flexibility of your Odoo modules. As you embark on your Odoo development journey, leverage the power of XML to tailor the system to your unique business requirements.