Skip to Content
Menu
This question has been flagged

I have made a link between contacts and the employee model; customised the create and write functions. I want to ensure that when basic details such as name, phone, mobile, and email are synced between the two records when updated from either side. Current the updates are working after a few attempts thereafte they stop because the context is not being updated properly and causing a recursion error.


The write code in the extended hr.employee

def write(self, vals):

        # Avoid recursion if the update is already in progress

        if self.env.context.get('skip_partner_update', False):

            return super(HrEmployee, self).write(vals)


        # Proceed with the write operation

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


        # Fields to synchronize with partner

        fields_to_sync = ['name', 'phone', 'mobile', 'email', 'job_id']

        related_fields = {

            'name': 'name',

            'phone': 'phone',

            'mobile': 'mobile',

            'email': 'email',

            'job_id': 'job_description',

        }


        # Propagate changes to related partner

        for employee in self:

            if employee.partner_id:

                partner_vals = {related_fields[field]: vals[field] for field in fields_to_sync if field in vals}

                if partner_vals:

                    # Set the context flag to avoid recursion during the partner update

                    employee.partner_id.with_context(skip_employee_update=True).write(partner_vals)


        return res


the write code in the extended res.partner
def write(self, vals):

        # Avoid recursion if the update is already in progress

        if self.env.context.get('skip_employee_update', False):

            return super(ResPartner, self).write(vals)


        # Proceed with the write operation

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


        # Fields to propagate to employees

        fields_to_sync = ['name', 'phone', 'mobile', 'email', 'job_description']

        related_fields = {

            'name': 'name',

            'phone': 'work_phone',

            'mobile': 'mobile_phone',

            'email': 'work_email',

            'job_description': 'job_id',

        }


        # Propagate changes to related employees

        for partner in self:

            for employee in partner.employee_ids:

                employee_vals = {related_fields[field]: vals[field] for field in fields_to_sync if field in vals}

                if employee_vals:

                    # Set the context flag to avoid recursion during the employee update

                    employee.with_context(skip_partner_update=True).write(employee_vals)


        return res

Avatar
Discard
Related Posts Replies Views Activity
4
Aug 24
1272
1
Jul 24
1176
0
Feb 16
6113
3
Jul 25
1670
1
Jun 25
1667