Creating scheduled actions in Odoo
Intermediate
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.
# -*- 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!
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.
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:
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.
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
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.
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):
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.
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.