Enable Dark Mode!
automated-action-in-odoo.png
By: Saritha

Automated action in Odoo

Technical

Automated actions can be used to automatically trigger actions based on a time condition. We can use them to automatically perform some operations on records that meet certain criteria and reach time conditions. Schedulers are automated actions that run automatically over a time period and can do a lot of things. They execute actions without manual interaction and make background job very easy: For this simply insert a record to the ir.cron table and Odoo will execute it as defined.

1. Creating the model and fields

The first step is to create a model (if you donโ€™t have one yet) and to create some fields on this model:

 
class ModelName(models.Model):
   _name = 'model.name'
    fields_name = fields.Char(string=" ")
 
   def method_name(self):
       "Your code here"

2. Creating the views

Now that you have the database part ready it is time to create your view

 3. Creating the automated action

<?xml version="1.0" ?>
<odoo>
 <data noupdate="1">
   <record id="backup_scheduler" model="ir.cron">
     <field name="function">schedule_backup</field>
     <field name="interval_type">work_days</field>
     <field name="name">Backup scheduler</field>
     <field name="numbercall">-1</field>
     <field name="priority">5</field>
     <field name="doall">False</field>
     <field name="active">False</field>
     <field name="interval_number">1</field>
     <field name="model">db.backup</field>
   </record>
 </data>
</odoo>

An important thing to note with automated actions is that they should always be defined within a noupdate field since this shouldnโ€™t be updated when you update your module.

 

The first thing you notice is the data noupdate="1", this is telling Odoo that all code within this tag shouldnโ€™t be updated when you update your module.

<record id="unique_name" model="ir.cron">

The id is a unique identifier for Odoo to know what record is linked to which id. The model called (โ€œir.cronโ€) is the model specifically made by Odoo for all automated actions. This model contains all automated actions and should always be specified.

<field name="name">Name </field>

The next line is the name.

<field name="active" eval="True" />

Boolean value indicating whether the cron job is active or not.

<field name="user_id" ref="base.user_root"/>

This user id is referring to a specific user, in most cases, this will be base.user_root.

<field name="interval_number">1</field>

Number of times the scheduler is to be called based on the โ€œinterval_typeโ€

<field name="interval_type">days</field>

Interval Unit.

It should be one value for the list: minutes, hours, days, weeks, months.

<field name="numbercall">-1</field>

An integer value specifies how many times the job is executed. A negative value means no limit.

<field name="doall">1</field>

A boolean value indicating whether missed occurrences should be executed when the server restarts

<field name="nextcall" >2016-12-31 23:59:59</field> <!-- notice the date/time format -->

Next planned execution date for this job.

<field name="model" eval="'model.name '" />

The field model specifies on which model the automated action should be called.

<field name="function" eval="'method_name '" />

Name of the method to be called when this job is processed.

<field name="args" eval="" />

The arguments to be passed to the method.

<field name="priority" eval="5" />

The priority of the job, as an integer: 0 means higher priority, 10 means lower priority.


If you need any assistance in odoo, we are online, please chat with us.



1
Comments



Leave a comment



WhatsApp