How to Use Schedulers to Run a Function in Odoo15
Schedulers are one of the important terms used in odoo. It helps to configure operations based on rules in odoo. Here we are going to see how a scheduler can run a function in odoo.
Here, first, we assume the module purchase. The tree view of this module contains Vendor, date, status, etc details. Let us assume that there is a field amount total in the tree view. This amount total field is declared in py as a float field and initially, store=true is not given.
But actually, it is a ‘compute’ field, so store=true must be given. After changing the amount total field as store=true, the new records in the tree view contain values. But old records contain the amount total as zero. So we use the scheduler to run the compute function written in py for amount total for bringing values in the amount total field for old records. In most cases, store = true must be given for compute field. But consider this scenario in which i didn’t give store = true at first. so because of that from the list view, the form takes more time to open. So for that reason, i add store=true and thus i miss values if the amount total for old records. To regain all values, this scheduler is used
The compute function is as follows:
amount_total = 0
for rec in self:
for lines in self.order_line:
amount_total += lines.price_subtotal
So this function needs to be run using the scheduler for bringing values in all old records.
For that first of all create a separate py file for the scheduler.
from odoo import models, fields
_inherit = ‘material.issue’
def run_compute_total_scheduler(self): Material_issue_obj = self.env[‘material.issue’].search()
for rec in Material_issue_obj :
So this function will run and return.
The xml file for the scheduler is given below:
<record id=”run_compute_total_scheduler” model=”ir.cron”>
<field name=”name”>Run Compute Total</field>
<field name=”model_id” ref=”model_material_issue”/>
<field name=”user_id” ref=”base.user_root”/>
This is the rule based on schedulers runs. Here the interval number is 1. so the scheduler runs each day. First, when we go to the scheduled actions in the settings menu, we can see the created scheduler named ‘run_compute_total_scheduler()’.When we click on the button run manually, it runs and the compute function to calculate the amount total associated with this scheduler works. Thus it generates the value for the amount total for all old records. In this way, the schedulers can be used for running functions in odor. In urgent cases, if we miss some values for old records, the client also needs values for all records. So we can simply put these codes for running the function. After running the function, we can simply remove the code also if there is no use for the code.
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"Get a Free Quote