Skip to Content
Menu
This question has been flagged
1 Reply
2447 Views

Hi. 
I hope you are doing fine. 

I have a function "create_invoice(self)" in my model "projects.project"

it works fine when i call it from a button but it wont work with the cron job. Following is my xml. 

<record id='ir_cron_project_invoices_update' model='ir.cron'>
<field name='name'>Invoice Update All Projectsfield>
<field name='model_id' ref='model_projects_projects'/>
<field name='state'>codefield>
<field name='code'>model.create_invoice()field>
<field name='interval_number'>1field>
<field name='interval_type'>minutesfield>
<field name='numbercall'>-1field>
<field name="doall" eval="True"/>
record>

i am not posting the function code its pretty messed up but for you to have an idea
suppose following is my function code

def create_invoice(self):
print('self', self)
for rec in self:
print('rec', rec)


i noticed that there is nothing in self when i call it from the cron can you tell me what i am doing wrong ? 

Thanks :) 
Muneeb.

Avatar
Discard
Author

Hi, @Cybrosys Techno Solutions Pvt.Ltd 24 November 2022Thanks for answering.
I guess I cant reply you my maybe cause my Karma is Low
I tried your method but i am getting the following the error.

Traceback (most recent call last): File "/opt/odoo16/odoo/http.py", line 1549, in _serve_db return service_model.retrying(self._serve_ir_http, self.env) File "/opt/odoo16/odoo/service/model.py", line 134, in retrying result = func() File "/opt/odoo16/odoo/http.py", line 1578, in _serve_ir_http response = self.dispatcher.dispatch(rule.endpoint, args) File "/opt/odoo16/odoo/http.py", line 1775, in dispatch result = self.request.registry['ir.http']._dispatch(endpoint) File "/opt/odoo16/odoo/addons/base/models/ir_http.py", line 140, in _dispatch result = endpoint(**request.params) File "/opt/odoo16/odoo/http.py", line 673, in route_wrapper result = endpoint(self, *args, **params_ok) File "/opt/odoo16/addons/web/controllers/dataset.py", line 46, in call_button action = self._call_kw(model, method, args, kwargs) File "/opt/odoo16/addons/web/controllers/dataset.py", line 33, in _call_kw return call_kw(request.env[model], method, args, kwargs) File "/opt/odoo16/odoo/api.py", line 461, in call_kw result = _call_kw_multi(method, model, args, kwargs) File "/opt/odoo16/odoo/api.py", line 448, in _call_kw_multi result = method(recs, *args, **kwargs) File "/opt/odoo16/odoo-custom-addons/project_management/models/projects.py", line 52, in create_invoice projects = self.env['project.project'].search([]) File "/opt/odoo16/odoo/api.py", line 537, in __getitem__ return self.registry[model_name](self, (), ()) File "/opt/odoo16/odoo/modules/registry.py", line 186, in __getitem__ return self.models[model_name] KeyError: 'project.project'

Author

Thanks Cybrosis I've found the error Your answer helped :))

Best Answer

Hi,

Here on the self you will not get records and instead of that you need to search records on projects as follows:

def create_invoice(self):
projects = self.env['project.project'].search([])
for rec in projects:
print('rec', rec)

Regards

Avatar
Discard