Odoo is renowned for its flexibility and power, making it an ideal platform for businesses to customize their workflows. One of the key features that enhance automation in Odoo is the Scheduled Action (also known as Automated Actions). These allow you to run specific operations at scheduled intervals, improving efficiency and reducing manual overhead. While Odoo’s interface provides an intuitive way to create scheduled actions, developers often prefer handling this through code for greater control and customization.
In this blog, we’ll dive into how to create scheduled actions programmatically in Odoo. By the end, you’ll be able to automate tasks like sending reminders, updating records, and more—all from the comfort of your custom module.
Prerequisites
Before we start, ensure you have:
- Basic Knowledge of Odoo Development: Familiarity with Odoo’s model architecture and Python programming.
- Development Environment: A working Odoo instance with access to create and deploy custom modules.
Step-by-Step Guide
1. Create a Custom Module
First, you need a custom module where you’ll define your scheduled action. If you don’t already have a custom module, create one.
- Generate Module Skeleton: Use Odoo’s scaffolding tool to create a new module. Run the following command in your terminal:bashCopy code
odoo scaffold my_scheduled_action_module addons
This command creates a basic module structure in theaddons
directory. - Define the Module: Edit the
__manifest__.py
file in your module folder to define the module’s metadata.pythonCopy code{ 'name': 'My Scheduled Action Module', 'version': '1.0', 'category': 'Tools', 'summary': 'Module for creating scheduled actions programmatically', 'depends': ['base'], 'data': [ 'data/scheduled_action_data.xml', ], 'installable': True, }
2. Create the Scheduled Action
Scheduled actions are defined as ir.cron
records in Odoo. You’ll need to create an XML file to define these records.
Create XML File: Inside your module, create a directory named data
and add a file named scheduled_action_data.xml
<?xml version="1.0" encoding="UTF-8"?> <odoo> <!-- Define the scheduled action --> <record id="my_scheduled_action" model="ir.cron"> <field name="name">My Scheduled Action</field> <field name="model_id" ref="model_my_model"/> <field name="state">code</field> <field name="code">model.my_scheduled_action()</field> <field name="interval_number">1</field> <field name="interval_type">days</field> <field name="numbercall">-1</field> <field name="active" eval="True"/> </record> </odoo>
Add the Model Reference: Ensure you have a model defined in your module. For this example, let’s assume you have a model called my.model
.pythonCopy codefrom odoo import models, fields, api class MyModel(models.Model): _name = 'my.model' _description = 'My Model' name = fields.Char('Name') @api.model def my_scheduled_action(self): # Your logic here records = self.search([('state', '=', 'draft')]) for record in records: record.state = 'done'
my_scheduled_action(): This method will be executed by the scheduled action. It searches for records in the my.model
model and updates their state.
3. Update the Module
After defining your scheduled action in XML and adding the necessary code, update your module to apply the changes.
- Restart Odoo: Restart the Odoo server to load your new module changes.bashCopy code
sudo service odoo restart
- Update the Module: Go to the Apps menu, search for your custom module, and click Upgrade.
4. Verify the Scheduled Action
To ensure your scheduled action is working correctly:
- Check Scheduled Actions: Navigate to Technical > Automation > Scheduled Actions in Odoo’s backend. You should see your scheduled action listed.
- Monitor Execution: Confirm that the action runs as expected by monitoring the logs or the results of the action.
Conclusion
Creating scheduled actions through code in Odoo offers flexibility and power, allowing you to automate complex processes efficiently. By following these steps, you can set up automated tasks tailored to your business needs, enhancing productivity and streamlining operations.
Feel free to extend this basic setup by adding more complex logic or integrating additional features. As with any customization, thorough testing in a development environment is crucial before deploying to production. Happy coding!