Hi. I'm trying to create a method that will be triggered whenever a field is changed, and I want to make this change on every contact of a company. To be specific, SLA agreement is a Many2one field. You can give any user any SLA agreement. My problem is, that I want to choose an agreement for a client company, and at the same time every contact (child) this company has. Here is the code:
class WebsiteSupportUpgradePartners(models.Model):
_inherit = "res.partner"
# sla_id = fields.Many2one('website.support.sla', string="SLA")
@api.onchange('sla_id')
def sla_inheritence(self):
if self.company_type == 'company':
_logger.info(f'LOG MESSAGE 1 {self.child_ids}')
if self.child_ids:
for child in self.child_ids:
company_contact = self.env['res.partner'].search([('id','=',child.id)])
_logger.info(f'LOG MESSAGE 2 {child.id}')
_logger.info(f'LOG MESSAGE 3 {company_contact}')
_logger.info(f'LOG MESSAGE 4 {company_contact.sla_id}')
# company_contact.sla_id = self.sla_id
company_contact.sla_id = self.env['website.support.sla'].search([('id','=',self.sla_id.id)])
_logger.info(f'LOG MESSAGE 5 {company_contact.sla_id}')
else:
_logger.info(f'LOG MESSAGE 6 {self.sla_id}')
self.sla_id = self.parent_id.sla_id
_logger.info(f'LOG MESSAGE 7 {self.sla_id}')
sla_id is definied fine, all others fields also, the part I do not understand is why changes are not saved? Loggers show me, that this work, message 4 shows False, which is correct, and message 5 shows valid sla. But no changes appear on company contacts. Commented part do not work as well. What is even more interesting, else (where mg 6 and 7 are) works fine, and when I tried to change SLA to different then it's parent company it sets again to the right one. So what am I missing, why logs show me that code worked where it didn't?
I tried with combinations of @api.multi, and @api.depends, still didn't work. Yes, I know that commented variable is named sla_id, but it's not it. This variable is in original module, besides even if uncommented it's still not working.