Automated or computerized actions that are scheduled to run automatically over time are called scheduled actions. One of the best features of Odoo is the automated running of processes, which is the primary purpose of Scheduled Actions. The term "crown jobs" also refers to scheduled actions.
In this blog, we will learn about the settings and functionality of the new Odoo 16 platform's Scheduled Actions.
Creating methods and models:
from odoo import models, fields
class MailActivity(models.Model):
_name = 'mail.activity'
def action_done(self):
print("function")
Create Scheduled Actions:
Using the following code we have created a scheduled action:
<data noupdate="1">
<record id="ir_cron_scheduler_recurring_action" model="ir.cron">
<field name="name">Recurring Todo Activity</field>
<field name="model_id" ref="model_mail_activity"/>
<field name="state">code</field>
<field name="code">model.action_done()</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>
</record>
</data>
Let's see how the aforementioned code is applied to the Odoo platform to define the Scheduled Actions.
<data noupdate="1">
Let's now comprehend how the aforementioned code is applied to the Odoo platform's Scheduled Actions.
<data noupdate="1">
After installation, noupdate="1" won't be updated; instead, it will be initialized. The automated actions are always specified in a no update field as well.
<record id="ir_cron_scheduler_recurring_action" model="ir.cron">
Each record's ID serves as a unique identifier, and that record will be kept in it. cron table.
<field name="name">Recurring Todo Activity</field>
Assign a name for the Scheduled action. That we can see in the UI and should give a meaningful name to the field.
<field name="model_id" ref="model_mail_activity"/>
This specifies on which model is the automated action called in.
<field name="state">code</field>
<field name="code">model.action_date()</field>
Here, we need to specify the method name to be called.
<field name="user_id" ref="base.user_root"/>
In this, the user ID refers to a specific user. In most cases base.user_root is used.
<field name="interval_number">1</field>
<field name="interval_type">days</field>
name=”interval_number” specifies that the number of times scheduled action is to be called based on interval_type
interval _type is the interval unit
<field name="numbercall">-1</field>
It is an integer value that indicates the frequency of execution of the action.
Put it to -1 if we want the action to continue indefinitely. For instance, There are interval types and interval numbers in XML. How can I set it to operate at a certain time of day?
Answer:
<field name="nextcall" eval="(DateTime.now()
timedelta(days=1)).strftime('%Y-%m-%d 12:00:00')" />
<field name="interval_type">days</field>
at 12 o’clock every day it will be executed.
Configure Scheduled actions in UI:
After enabling developer mode, navigate to Settings > Technical > Automation > Scheduled Actions.
The Scheduled actions link. The already established scheduled actions are displayed in a tree format. You can choose any of those to view the form view.
By selecting the Create button, a new planned action can be created.
Input the scheduled action model, scheduled user, number of calls, priority, and other details. Additionally, we can directly set the Python method from this form view. If you choose a specific time for this method to run, it will do so; if not, we can manually run it by selecting the Run manual button. In this manner, Odoo 16 users can establish scheduled actions.