Home > Blockchain >  How to update a record from another function
How to update a record from another function

Time:06-26

I'm trying to update the body field from the payrun_chatter_log() but it won't update. any idea on how to do this? This is what I did:

def payrun_chatter_log(self):
        subtype = self.env['mail.message.subtype'].search([('id','=', '2')])
    
        vals = {
            'date': fields.datetime.now(),
            'email_from': self.env.user.email_formatted,
            'author_id': self.env.user.id,
            'message_type': 'notification',
            'subtype_id': subtype.id,
            'is_internal': True,
            'model': 'custom.module',
            'res_id': self.id,
            'body': 'Test'
        }
        self.env['mail.message'].create(vals)
    
def custom_button(self):
        chatter = self.env['mail.message'].search([('res_id', '=', self.id)])
        message = 'Custom Message here'
        chatter.update({'body': message})
        return super(CustomModule, self).custom_button()

CodePudding user response:

Your below line of code in custom_button will return multi record because res_id will be repeated for more than one model

chatter = self.env['mail.message'].search([('res_id', '=', self.id)])

You need to add the model to search:

chatter = self.env['mail.message'].search([('model', '=', self._name), ('res_id', '=', self.id)])

CodePudding user response:

The message body should be updated, you need to refresh the page to see the changes.

Try to return the following client action:

return {
    'type': 'ir.actions.client',
    'tag': 'reload',
}

The chatter has an One2many relation to mail.message model. Your function will update all messages

  • Related