Creating scheduled actions in Odoo

< / / / / / >
Intermediate
Framework
V13.0
V 13.0
+/- 12 minutes
Written by Yenthe Van Ginneken
(0)

Quick scroll

1. Introduction

Scheduled actions are used to automatically execute code in Odoo. Odoo has added the ability to create scheduled actions and configure how often they should go off to execute your custom code. scheduled actions - also known as schedulers - are a great way to automatically update data or do operations on data within Odoo. In this tutorial you will learn how to create an scheduled action. We will create a new model, menuitem, view and scheduler to show you how the scheduled actions work.

2. Creating the model

The first thing that we need is a model. Let's create a new model named 'scheduler.demo' add a name field for the record names and an integer field to keep track of how often our scheduler went off. Create a new Python file named 'scheduler_demo.py' under the 'models' directory of your custom app. Now create a new model and add two fields to it:
                    # -*- coding: utf-8 -*-from odoo import models, fields, apiclass SchedulerDemo(models.Model):    _name = 'scheduler.demo'    name = fields.Char(required=True)    number_of_updates = fields.Integer('Number of updates')                                    

This is all you need on the database level. You have your fields in the database so you only need to show them to the user now!

3. Creating the views

Let's create three views: a list view, a form view and a search view. In the list view we want to show the fields 'name' and 'number_of_updates' so we can get a quick overview. In the form view we want to show the fields 'name', 'number_of_updates' and 'write_date' so we can see how often the scheduler went off and when the record was last updated. The field 'write_date' is a built-in field in Odoo that is available on any model by the way. Finally we want a search view so that we can quickly search and find records.

3.1 Creating the list view

Let us start by creating a list view and adding the fields 'name' and 'number_of_updates':
                                                                                

3.2 Creating the form view

Now let us create a form view that shows the fields 'name', 'number_of_updates' and 'write_date':
                                                                                

3.2 Creating the search view

Finally, add a search view for easily finding the records by 'name' and 'number_of_updates':
                                                                                

4. Creating the scheduled action

4.1 Intro and code

Now that our model and views are ready we need to create the scheduled action itself. Every scheduled action that you create in Odoo will be saved as a record in the database, on the model 'ir.cron'. This 'ir.cron' record will hold all the values so that Odoo knows what exactly you want your scheduler to do. Create a new XML file named 'scheduler_data.xml' under the 'data' folder of your custom module.

An important thing to note with scheduled actions is that they should always be defined within a noupdate field since this record shouldn't be updated when you update your module. So let us create an scheduled action that runs once every day for the model 'scheduler.demo'. A last criteria is that the scheduled action calls a Python function from where we can do our operations on the records in the database. Have a look at the following code:

                                                                                

When you would install your module this would give the following scheduled action: scheduled action result

4.2 Diving deeper into the scheduled action code

Lets break down the code step by step so that we know exactly what it does. If you don't want to read this you can scroll to the next chapter.The first thing you'll 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. The next thing you'll notice is the record id and model.
                                                                                
  • name: the name will be shown on the scheduled action and is what the user will see.
  • model_id: tells Odoo on which model the scheduled action should be executed.
  • state: tells Odoo what action this scheduled action should do. This can be 'code', 'object_create', 'object_write', 'multi', 'email', 'followers' or 'next_activity'.
  • code: links the scheduler to a Python function. It should always start with 'model.' and then the name of your Python function followed by ().
  • active: if set to True the scheduler will always run automatically, if set to False the scheduler will not go off automatically.
  • user_id: with with user the scheduled action should be executed.
  • interval_number: how often the scheduler should go off.
  • interval_type: in which period it should go off. Possible values are 'minutes', 'hours', 'days', 'weeks', 'months'.
  • number_call: tells Odoo how often this automated action should be run. If you would fill in '10' then the action would run 10 times. When you want it to run forever you simply fill in '-1', which is the same as infinite.

4.3 writing the Python function for the scheduler

Great, we now have the scheduled action! Now all we need is the Python function that we call in our scheduled action so that we can do operations on our records. Create a new function named 'process_demo_scheduler_queue' in your 'scheduler_demo.py' file that searches all records on our table and then updates the records:

                        @api.model    def process_demo_scheduler_queue(self):        # Contains all records for the model scheduler.demo        scheduler_demo_records = self.env['scheduler.demo'].search([])        # Loop over the records one by one        for demo_record in scheduler_demo_records:            number_of_updates = demo_record.number_of_updates + 1            # Update the record with the new number of updates            demo_record.write({                'number_of_updates': number_of_updates            })                                    

This function will loop over all records found in the table 'scheduler.demo' and will update the field 'number_of_updates' by adding 1 to the existing value. If you now execute this scheduler you will see that the field 'number_of_updates' is no longer 0 but 1. From here on you can do anything you like! You can call models, search values, update values, do calls to external systems, ... whatever you need.

5. Creating a security rule

Now that we have the views and code in place we'll need a security rule in the 'ir.model.access.csv' file to be able to access the view and create records in it. Let's create a simple security rule that gives all users full rights on this model:
                    id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlinkaccess_scheduler_demo,scheduler.demo.example,model_scheduler_demo,,1,1,1,1                                    

6. Creating the menuitem and action to open it

Alright, we're almost done! Now that we have the model, the views and the security rule we just need a menuitem with an action in order to open the view(s). In this tutorial I'll create a new app on the homescreen with a menuitem and an action as I do not know where you'd want to build your views/menus. You can just change this example.

6.1 Creating the menuitems

First we'll need a main menu item, which will show up as an app on the Odoo home screen. Just create a new menuitem with a name and an icon:

                                                                                

Now also create a submenu and add an action to this menu (so that we can click on it):

                                                                                

6.2 Creating an action

The last step is to create an action. The action will make sure that if you click on the menuitem that it will open up our example view:

                                                                                

That's it! If you now install this module you'll have fully working views and a scheduled action named 'Demo scheduler'. If you run this scheduler - from Settings > Technical > Automation > Scheduled actions - you will see that the records are being updated. Whenever your scheduler goes off automatically the same will happen as if you've manually clicked on the scheduler.

7. Conclusion

Scheduled actions are a very powerful tool in Odoo and the options are almost unlimited. Scheduled actions are ideal for automatically doing things in Odoo without having to do them manually. It is a great feature in Odoo to save you time and to make things go smoother. As they are so powerfull you should always test your actions on a test system to be sure everything is working as expected.