I am using Odoo 11 and want to notify the user when a certain record is created, not via email but inside Odoo such that there is this thing:
I think this should be fairly simple with Odoo standard features but I don't know how to do it.
What I tried is an Automated Action that adds the users that should be notified as followers (Action To Do: Add Followers,Trigger Condition: On Creation).
Further I inherit in my model from mail.thread
, track several fields, and define a Subtype for them. And this does indeed work to be notified about changes of the fields, but there is no message when the record is created. Why is this? Maybe creation does not count as change? Or Maybe the Automated Action is executed to late because he must be following BEFORE the record is created?
An alternative that I see would be to overwrite the create(...) method and send some message from there. But how to do this? It feels like there is something obvious that I don't see. I mean there is a note that the record was created in the chatter anyway. All I want to do is to have this in a users inbox as message.
Example Code:
class MyModel(models.Model):
_name = 'my_module.my_model'
_inherit = ['mail.thread', 'mail.activity.mixin']
name = fields.Char(string='Name', track_visibility=True)
def _track_subtype(self, init_values):
if 'name' in init_values:
return 'mail.mt_comment'
return super(Alert, self)._track_subtype(init_values)
I found a solution. First step is to add an Automated Action that adds the followers to newly created records. Sadly this action is performed after the record is created, such that the messages about the creation will not be sent to the followers.
My solution to this is to send another message about the creation in the write method, but of course only for newly created records, which I detect using a boolean flag.
Here is the code:
class MyModel(models.Model):
_name = 'my_module.my_model'
_inherit = ['mail.thread', 'mail.activity.mixin']
name = fields.Char(string='Name', track_visibility=True)
newly_created = fields.Boolean('Newly Created')
@api.model
def create(self, values):
values['newly_created'] = True # Set the flag to true for new records
return super(Alert, self).create(values)
@api.multi
def write(self, values):
res = super(MyModel, self).write(values)
if(self.newly_created):
self.message_post(body=_('Created New Record'), subtype='mail.mt_comment', author_id=self.create_uid.partner_id.id)
# Set the flag to false so we post the message only once
self.newly_created = False
An important detail is that super(MyModel, self).write(values)
must come before posting the message and before updating the flag.
Note that this write message will be called by Odoo directly after the model creation because the Automated Action adds the followers to the newly created record. So this works hand in hand for me now but only if there is such an Automated Action.