Search code examples
pythonodooodoo-16

Send notification from cron action in Odoo 16


I have a recurring CRON action in my app. When fired it updates some records in the database. I want to inform the creator of the record that it has been updated by the CRON task. I've tried many variations of the code below but no message is sent. There is no error thrown.

def _send_discuss_message(self, plan):
    recipient_id = plan.create_uid.id
    channel = self.env['mail.channel'].channel_get(
        [recipient_id])
    user_id = self.env.user.id
    message = ("Your plan %s has been validated") % (plan.name)
    channel_id = self.env['mail.channel'].browse(channel["id"])
    channel_id.message_notify(
        author_id=user_id,
        partner_ids=[recipient_id],
        subject="Test",
        body=(message),
        message_type='comment',
        subtype_xmlid="mail.mt_comment",
        notify_by_email=False
        ) 

Solution

  • The message_notify function expects a list of partner_ids to use to create a mail message record

    You probably notified a partner with the same ID, try using the related partner ID instead:

    recipient_id = plan.create_uid.partner_id.id