Creating Cron and Server Action in Odoo 11

For creation the Cron in odoo11, you will have also have to set model_id fields also as now in odoo11 ir.cron  have the delegate based relationship with ir.actions.server in which the  model_id is set to required.

Below code is explaining the delegate based relation between ir.cron and ir.actions.server.

 

class ir_cron(models.Model):
	_name = "ir.cron"
	_order = 'cron_name'

	ir_actions_server_id = fields.Many2one(
		comodel_name = 'ir.actions.server', 
		string = 'Server action',
		delegate = True,
		ondelete = 'restrict', 
		required = True
	)
	....
	....

class IrActionsServer(models.Model):
	_name = 'ir.actions.server'
	_table = 'ir_act_server'
	_inherit = 'ir.actions.actions'

	model_id = fields.Many2one(
		comodel_name = 'ir.model', 
		string = 'Model', 
		required = True, 
		ondelete = 'cascade',
		help = "Model on which the server action runs."
	)
	....
	....

For explaining the example I am creating a new a model (my.task) and registering  @api.model decorated method  cron_do_task.

class MyTask(models.Model):

	_name = "my.task"

	@api.model
	def cron_do_task(self):pass	
		....
		....

 

Now create data XML file for ir.cron  which should look like:

<record id="cron_do_task" forcecreate='True' model="ir.cron">
      <field name="name">Do -Task</field>
      <field eval="False" name="active"/>
      <field name="user_id" ref="base.user_root"/>
      <field name="interval_number">15</field>
      <field name="interval_type">minutes</field>
      <field name="numbercall">-1</field>
      <field name="model_id" ref="model_my_task"/>
      <field name="state">code</field>
      <field name="code">model.cron_do_task()</field>
</record>

Here the model_id is the model in which you want to execute the code section (for now @api.model decorated method cron_do_task ).

 

For creation the Server Action in odoo11 follow the below example:

Update your python code and add @api.multi decorated method ( server_do_action ):

class MyTask(models.Model):

	_name = "my.task"
	
	@api.model
	def cron_do_task(self):pass	
	
	@api.multi
	def server_do_action(self):pass	
		....
		....

 

and create data XML file for  ir.actions.server  as below:

    <record id="do_task_server" model="ir.actions.server">
          <field name="name">Do Action</field>
          <field name="type">ir.actions.server</field>
          <field name="model_id" ref="model_my_task"/>
          <field name="state">code</field>
          <field name="code">
              if records:
                  action = records.server_do_action()
          </field>
          <field name="binding_model_id" ref="model_my_task"/>
      </record>

 

Here model_id  is the model on which this action will show and  binding_model_id is the model which code section (records.server_do_action()) will execute on the click of server action.

 

That’s all for today. I hope this blog will help you. I’d be very grateful if you’d write your opinions, comments, and suggestions to keep the page updated and interesting.

You may also like our post on Performing String and Bytes Data Conversion in Python3.x.

. . .

Ask a Feature