Create Schedule action in Odoo

Posted By : Aman Sharma | 31-Jan-2023

Create Schedule Action in Odoo

 

STEP 1 : Create a model and action in the custom module

The following code will help you to create a model and action in the custom module:

class StockPicking(models.Model):

_inherit = 'stock.picking'

 

def unreserve_stock(self):

records = self.env['stock.picking'].sudo().search([('state', '=', 'assigned')])

for rec in records:

rec.do_unreserve()

 

STEP 2 : Define the view for the custom module

STEP 3 : Create scheduled actions

Using the following code you can create a scheduled action:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">

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

<field name="name">Un-Reserve Stock</field>

<field name="model_id" ref="model_stock_picking"/>

<field name="state">code</field>

<field name="code">model.unreserve_stock()</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 eval="False" name="doall" />

</record>

</data>
</odoo>

 

Let me explain the above xml code in detail :

<data noupdate="1">

This indicates that all code within this tag shouldn’t be updated when we update your module.

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

id is the identifier and must be unique, ir.cron is the model which is used to handle all the cron jobs therefore, specify it after model

<field name="name">Un-Reserve Stock</field>

This is what users can view from the UI. So let’s take care to specify a meaningful name for the field.

<field name="model_id" ref="model_stock_picking"/>

Indicates on which model the automated action should be called.

<field name="code">model.unreserve_stock()</field>

Here you need to specify the method name to be called.

<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>

Always a number and here we specify the number of times the scheduler is to be called based on the “interval_type”. In this example, the cron job is called daily

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

The possible values are : minutes, hours, days, weeks, months.

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

Scheduled actions are called based on the number specified here. When you want it to run forever you simply put ‘-1’.

<field eval="False" name="doall" />

It’s a boolean field. If True : missed occurrences should be executed when the server restarts.

About Author

Author Image
Aman Sharma

Aman is an accomplished Backend developer with extensive industry experience, specializing in Odoo technology. He has a profound understanding and expertise in Python, Odoo ERP, Flask, MySQL, PostgreSQL, JavaScript, and payment gateway integration, particularly with Stripe. Aman's exceptional contributions to projects like Mymandi, Pando store, Markivia, DataInsite, and Tundra have been instrumental in driving the growth of his company. His strong analytical skills are pivotal in ensuring the success of his work, as he is able to identify and solve complex problems efficiently. Beyond his professional endeavors, he possesses a genuine passion for learning new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..