This question has been flagged
1 Reply
3495 Views

We are creating a custom module that will help new employees through their first days on the job. We have our own models based on 'hr.employee' and 'hr.plan' (among others). 

When we try and launch a plan for a new employee, the user_id field has to be filled in. This means that there are some extra steps needed in the UI to create a record of the res.user model. To reduce the number of steps needed before launching a plan, we would like to automate this and have the user_id field be filled in automatically.


Does anyone know how this can be done in code instead of using the UI?

Avatar
Discard
Best Answer

Hi,

You can use the following code for creating a related user for the employee during his plan launching.

class HrPlanWizard(models.TransientModel):
_inherit = 'hr.plan.wizard'

def action_launch(self):
user_id = self.env["res.users"].create(
{"login": self.employee_id.work_email, "name": self.employee_id.name}
)
self.employee_id.update({'user_id': user_id.id})
return super(HrPlanWizard, self).action_launch()

In the above code, we are creating user with its require fields login and name, you can update the fields which you require in that dictionary.

Hope it helps

Avatar
Discard

I tried to make a new user but this is given to me when trying to create a user from custom module
"('You cannot create a new user from here.\n To create new user please go to configuration panel.', 72, 'Go to the configuration panel', None)"
any idea on how to skip this warning?