Integrated Tech Solutions

Creating Scheduled Actions in Odoo via Code: A Step-by-Step Guide

By - Admin August 11, 2024

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:

  1. Basic Knowledge of Odoo Development: Familiarity with Odoo’s model architecture and Python programming.
  2. 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.

  1. Generate Module Skeleton: Use Odoo’s scaffolding tool to create a new module. Run the following command in your terminal:bashCopy codeodoo scaffold my_scheduled_action_module addons This command creates a basic module structure in the addons directory.
  2. 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.

  1. Restart Odoo: Restart the Odoo server to load your new module changes.bashCopy codesudo service odoo restart
  2. 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:

  1. Check Scheduled Actions: Navigate to Technical > Automation > Scheduled Actions in Odoo’s backend. You should see your scheduled action listed.
  2. 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!

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »
next js deployed on aws

Beginners Guide to deploy NextJs App on AWS EC2 Server

In this blog post, we will walk you through the step-by-step process of deploying a Next.js application on an AWS...

Know More »