Skip to Content
Menu
This question has been flagged
1 Reply
3365 Views

I stuck with override model hr.employee.  I want to make field user_id (Related User) automatic change when we import data to hr_employee (work_email exists). Simply, Import a Employee => Related User will be the user who get same email with Work Email ( In Work Information - Employee).

Thanks and Regards,

Minh

Avatar
Discard
Best Answer

Hi,

What you can do for this is that, just override the create method the model hr.employee and assign the related user to the employee.

class HrEmployee(models.Model):    
    _inherit = 'hr.employee'
 
    @api.model
    def create(self, vals):
        # In the vals you will have the work email and all other details
        # compare the email with the mail of users
        user_rec = self.env['res.users'].search([('login', '=', vals['work_email'])], limit=1)
        vals['user_id'] = user_rec.id
        res = super(HrEmployee, self).create(vals)  
        return res

Just print the vals and see how to access the work email and user_id.  vals['work_email'] and  vals['user_id'], may not be the way to access it, just check and adjust accordingly

Thanks

Avatar
Discard
Author

Thanks, that is really a good answer.