Odoo - Python4 min read

How to create a Scheduled Action in Odoo 16

Habib Mohammadi

Habib Mohammadi

How to create a Scheduled Action in Odoo 16

In Odoo, a scheduled action is a configuration that automates specific tasks to run at defined intervals using the framework's internal cron-like mechanism. In today's post, we'll go through a straightforward way to create a scheduled action in Odoo 16 programmatically.

1. Create a New Module?

Before diving into the scheduled action, it's a good idea to create a new module to keep your customizations organized however you can also continue with an existing module.

  • Set up the basic structure of an Odoo module. If you're unfamiliar with this, check out Odoo's documentation on creating a basic module.

2. Define Your Method

Your scheduled action will run a specific method. Let's say you want to automate a task that reminds users of any overdue tasks:

from odoo import api, models

class CustomReminder(models.Model):
    _name = 'custom.reminder'
    _description = 'Custom Task Reminder'

    @api.model
    def remind_overdue_tasks(self):
        overdue_tasks = self.env['project.task'].search([('date_deadline', '<', fields.Date.today())])
        for task in overdue_tasks:
            # Send a reminder for each task. You can customize this as per your requirements.
            task.message_post(body="This task is overdue!")

3. Create the Scheduled Action

Now, let's set up the scheduled action. Create a new XML file in your module (e.g., data/scheduled_actions.xml) and define the action:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="ir_cron_remind_overdue_tasks" model="ir.cron">
        <field name="name">Remind Overdue Tasks</field>
        <field name="model_id" ref="model_custom_reminder"/>
        <field name="state">code</field>
        <field name="code">model.remind_overdue_tasks()</field>
        <field name="user_id" ref="base.user_root"/>
        <field name="interval_number">1</field>
        <field name="interval_type">days</field>
        <field name="numbercall">-1</field>
        <field name="doall" eval="False"/>
        <field name="active" eval="True"/>
    </record>
</odoo>

This XML defines a scheduled action that runs the remind_overdue_tasks method every day.

4. Integrate the Scheduled Action into Your Module

Make sure to add the XML file to the __manifest__.py of your module:

'data': [
    'data/scheduled_action.xml',
],

5. Install/Update Your Module

Now, install or update your module in Odoo, and the scheduled action will be created and automatically run at the specified interval.

Enjoyed this article?

Share it with your network

Habib Mohammadi

Written by Habib Mohammadi

Experienced web developer specializing in backend systems, database management, and creating seamless APIs.