How to Trigger Functions From XML Files In Odoo18
In Odoo framework, ‘XML’ files are generally used for customizing the views which enables creation of different types of structures and user-interfaces, while the functions are generally trigger functions xml files in odoo18 on some button click. While ‘XML’ is a powerful tool, some challenges to occur in real-world business scenarios when dealing with a wide range of data.
Also there may be instances when it is required to trigger a function or a specific action automatically when a custom module in the Odoo database. In such scenarios we can use the trigger functions xml files in Odoo18 file to invoke the required functions or actions.
In this blog, we can look into the steps involved in triggering a function from the ‘XML’ file.
In general, there are widely two different ways to trigger a function using ‘XML’ file, they are
- Triggering a function without arguments
- Triggering a function with arguments
Let us see an example of creating a new product using ‘XML’ with the help of above mentioned approaches.
Triggering A Function Without Arguments
The first step is to create a ‘.xml’ file in the custom module. In this method, we can call a function within a model without sending any arguments or parameters. The XML syntax for achieving this in the image below.
Within the tag, we will add a element which signifies a function call within the Odoo framework. In the above image, this means that the tag references a function named ‘_create_new_product’ in the model ‘product.product’. When this function it will carry out the specific action mentioned here.
Next step is to define the above function in the ‘.py’ file. The below image will show the contents of the ‘.py’ file.
Now the installation of the custom module will trigger functions xml files in Odoo18 the above function which will result in the creation of a product named ‘New Product’ in the database.
Triggering A Function With Arguments
As mentioned above, the first step is to create a ‘.xml’ file in the custom module. In this method, we can call a function within a model with some arguments or parameters. The XML syntax for achieving this in the image below.
As mentioned earlier, we have and tags. Also in the above image, we have a tag inside the containing ‘Table’. This specific value will as an argument to the function named ‘_create_new_product_with_parameters’.
Next step is to define the above function in the ‘.py’ file. The below image will show the contents of the ‘.py’ file
Now the installation of the custom module will trigger the above function which will result in the creation of a product named ‘Table’ in the database.
Thus, by utilizing this feature of ‘XML’ file, we can enhance the flexibility and usage of our custom modules in Odoo which can be also used to cater the specific needs of various business approaches.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote