Search code examples
pythonnotificationsodooodoo-15

Odoo 15 notification navigate back to task/record


I am trying to send notification to user A then user B Approves a task. (but using the odoobot user)

notification_ids = [(0,0,{'res_partner_id':user_id.partner_id.id,'notification_type':'inbox'})]

self.sudo().message_post(subject=summary,body=summary_link,message_type='notification, subtype_xmlid='mail.mt_comment', author_id=odoo_bot_id.partner_id.id, notification_ids=notification_ids)

it was working but now i am going this:

ValueError: Posting a message should be done on a business document. Use message_notify to send a notification to an user.

if self._name == 'mail.thread' or not self.id or message_type == 'user_notification':
            raise ValueError(_('Posting a message should be done on a business document. Use message_notify to send a notification to an user.'))

what does not self.id mean

according to this I should use

message_notify_ids = [user_id.partner_id.id]

record.sudo().message_notify(partner_ids=message_notify_ids, parent_id=odoo_bot_id.partner_id.id, author_id=odoo_bot_id.partner_id.id,  body=summary_link, subject=summary)

this shows the popup notification but does not show in the index so the user can navigate to the task

Odoo Form


Solution

  • When I logged self.id I got NewId_26404 which is a temp id since the function was run in a compute.

    Fix

    record = record if isinstance(record.id,int) else record._origin
    
    

    Note: discarding the changes did send the notification in my case, so it maybe better to add it in write function

    I am open to suggestions if there is a better solution thank.