This question has been flagged
2 Replies
5407 Views
I'm trying to create a cron job that saves subcriptions on Odoo database.
The "save_subscription" method works fine when called from a button, but when it is
called from the cron job, I get an empty recordset. I tried logging "self" and
get the following:

When called from cron:
SELF res.company()

When called from button:
SELF res.company(1,)

When executing the cron job, every model I try to access with self.env['model'] is also empty.
I've tried changing the decorator to @api.multi but no luck. I've also tried this answer https://www.odoo.com/pt_BR/forum/help-1/question/how-to-call-a-scheduled-task-cron-using-the-new-api-v8-90924 but I still get an empty recordset.

Can anyone tell me how to access my records through a cron job? I feel like I'm doing a silly mistake but I've tried everything and couldn't find it.

Thanks in advance!


@api.model
def cron_get_new_subscriptions(self):
    ...
    ...
    if subscriptions['items']:
       for sub in subscriptions['items']:
            splitDate = sub['createdAt'].split('T')[0].split('-') created_at = datetime(int(float(splitDate[0])),             int(float(splitDate[1])), int(float(splitDate[2]))).date()
            self.save_subscription(sub, created_at == today.date())
       return

@api.multi     
def save_subscription(self, item, isNew):
    _logger.info("\n\nSELF %s", self)
    ...
    ...

<record id="cron_get_new_subs" model="ir.cron">
    <field name="name">Cron Get New Subscriptions</field>
    <field name="user_id" ref="base.user_root" />
    <field name="interval_number">15</field>
    <field name="state">code</field>
    <field name="interval_type">minutes</field>
    <field name="numbercall">-1</field>
    <field eval="False" name="doall" />
    <field name="model_id" ref="model_res_company"/>
    <field name="code">model.cron_get_new_subscriptions()</field>
</record>
Avatar
Discard
Best Answer

Hello Arthur,

Cron job function self will give always empty browse object only

So you have search the company or you can get it from self.env.user.company_id

Avatar
Discard
Author

I see. And how would I access my self.env['sale.order'] from inside a cron, for example? How do I search for it?

Author

Nevermind, I got it. Basically I was trying to get access to a my model variables through "self.variableName", but since self was empty I got errors.

Thanks, subbarao!

Author

Nevermind, I got it. Basically I was trying to get access to a my model variables through "self.variableName", but since self was empty I got errors.

Thanks, subbarao!

Best Answer

Settings -> Activate the developer mode

Settings - Technical -> Automation -> Scheduled Actions

Select your cronjob -> Run Manually

You can use pycharm community to debug

Avatar
Discard