Search code examples
pythonxmlodoo-12

Execute a cron job from a method using python in odoo 12


I'd like to execute a Cron Job at the end of the method execution when I click a button, I read this post which mention to explicitly run a method, but it isn't working for me.

So far this is what I got:

my cron job

 <record id="ir_cron_save_timesheets" model="ir.cron">
            <field name="name">Save timesheets</field>
            <field name="model_id" ref="rw_project.model_rw_file_reader"/>
            <field name="type">ir.actions.server</field>
            <field name="state">code</field>
            <field name="code">model.process_ts()</field>
            <field name="interval_number">1</field>
            <field name="interval_type">days</field>
            <field name="numbercall">-1</field>
        </record>

in the button code at the end I want to execute this:

self.env.ref('project_name.ir_cron_save_timesheets').process_ts()

In the model, I do have a method name called process_ts

the error that I'm getting is:

AttributeError: 'ir.cron' object has no attribute 'process_ts'

If I remove process_ts it doesn't get executed


Solution

  • You are trying to call process_ts on a cron record which is wrong, the process_ts method is defined in the rw_project.model_rw_file_reader model.

    You have just to call the method_direct_trigger() and it should call the process_ts() method:

    self.env.ref('project_name.ir_cron_save_timesheets').method_direct_trigger()