Define schedule actions Odoo 9

Schedule actions can be set by adding this code in your xml file. This exemple will call the function schedule_action from the model hr.employee and send a notification by email if the boolean enable_send_notification is True.

<record id="ir_cron_schedule_action" model="ir.cron">
   <field name="name">Schedule actions</field>
   <field name="user_id" ref="base.user_root" />
   <field name="interval_number">1</field>
   <field name="interval_type">days</field>
   <field name="numbercall">-1</field>
   <field eval="False" name="doall" />
   <field eval="'hr.employee'" name="model" />
   <field eval="'schedule_action'" name="function" />
</record>

This model inherits hr.employee and defines 1 additional field, and 2 additional functions. send_notification is sending email based on the the template my_module.email_hr_employee_notification, see this post how to create email template with xml, schedule_action is the function called by the scheduler.

import logging
import openerp.addons.decimal_precision as dp
from datetime import datetime, date, timedelta
from openerp.exceptions import UserError
from openerp import models, fields, api, _

_logger = logging.getLogger(__name__)
    
class HrEmployee(models.Model):
    _inherit = 'hr.employee'
    
    enable_send_notification = fields.Boolean(string='Send notification')

    @api.multi
    def send_notification(self):
        self.ensure_one()
        ir_model_data = self.env['ir.model.data']
        template_obj = self.env.ref('my_module.email_hr_employee_notification')
        template_obj.send_mail(self.ids[0], force_send=True)
        return True
    
    def schedule_action(self, cr, uid, context=None):
        hr_employee_obj = self.pool.get('hr.employee')
        hr_employee_ids = self.pool.get('hr.employee').search(cr, uid, [])
        for hr_employee_id in hr_employee_ids:
            hr_employee = hr_employee_obj.browse(cr, uid, hr_employee_id , context=context)
            if hr_employee.enable_send_notification:
                hr_employee.send_notification()

2 thoughts on “Define schedule actions Odoo 9”

  1. can you share the code to this, some files are missing and I keep getting this error Uncaught Error: QWeb2: Template ‘Pos2SoWidget’ not found

Leave a Reply

Your email address will not be published. Required fields are marked *