This question has been flagged

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.

Avatar
Discard
Author Best Answer

Thanks for answers, but none of them work. However, I found the solution on my own. Here it is:

class WebsiteSupportUpgradePartners(models.Model):
_inherit = "res.partner"

@api.multi
@api.onchange('sla_id')
def sla_inheritence(self):
if self.company_type == 'company':
if self.child_ids and self.sla_id:
parent_sla = self.env['website.support.sla'].search([('id', '=', self.sla_id.id)])
for child in self.child_ids:
child.write({'sla_id': parent_sla.id})

I don't really know why previous code didn't work, even if logs showed otherwise, but this one is doing fine. For me, issue is closed.

Avatar
Discard
Best Answer

Hello Maks,

Yes it is possible.

But make sure you override the existing create and write function of the corresponding model

For Example:

class ProductTemplate(models.Model):

     _inherit = "product.template"

     custom_field = fields.Char("Custom Field", readonly=True)


     @api.onchange('ref_field')
     def onchange_value(self):

         if self.ref_field:

             self.custom_field = "Your Value"


     @api.model

     def create(self, vals):

         vals['custom_field'] = "Your Value"

         res = super(ProductTemplate, self).create(vals)

         return res


     @api.multi

     def write(self, vals):

         vals['custom_field'] = "Your Value"

         res = super(ProductTemplate, self).write(vals)

       return res.

Hope it helps,
Mayank

Avatar
Discard
Best Answer

Hello Maks,

Try the following code

@api.multi

@api.onchange('sla_id')

def sla_inheritence(self):

        if self.company_type == 'company':

                if self.child_ids and self.sla_id:

                        for child in self.child_ids:

                                child.sla_id = self.sla_id.id

Avatar
Discard