Skip to Content
Menu
This question has been flagged
2 Replies
9024 Views

How to get the job_id of the user_id linked to the employee_id

Avatar
Discard
Best Answer

Hi,

If you have a user id with you, then if you wan to find the job _id of the employee whose related user is this user_id, you can do like this,

suppose uid has the id of the user, then

employee_rec = self.env['hr.employee'].search([('user_id', '=', uid)])
job_id = employee_rec.job_id.id

If the user is assigned to more than one employee, then you have to use for loop to iterate over the employee_rec or use limit=1 in search condition.

If you have the employee id with you, then you can get the job id easily , let emp_id is the id of employee

employee_rec = self.env['hr.employee'].search([('id', '=', emp_id)])
job_id = employee_rec.job_id.id

To get the current user name and his job name in the qweb , try the below code.

<span t-esc="user.name"/>
<t t-foreach="request.env['hr.employee'].search([('user_id', '=', user.id)])" t-as="obj">
<t t-esc="obj.job_id.name"/>
</t>

Thanks


Avatar
Discard
Author Best Answer

I have this NameError: name 'self' is not defined

my_employee = self.env['hr.employee'].search([('user_id', '=', create_uid)])

my_job_id = my_employee.job_id.id
Avatar
Discard

From where you calling this code ? Try to print self and see whats there in it

Author

class PurchaseOrder(models.Model):

_inherit = 'purchase.order'

my_employee = self.env['hr.employee'].search([('user_id', '=', create_uid)])

my_job_id = my_employee.job_id.id

only this much lines ? can you add the full code in this class ? not defined any function ?

Author

yes only this much lines! and no i dont defined any function!!! i'm just trying something fast and want to get the job id but im looking for xml solution too if you can help

Author

i need to give job_id to this <span t-field="o.create_uid.name"/>

Actually you can't do it like this,

can you tell what exactly you are looking for

Author

in a report i want to mention the user who created it and his job

For getting the current user name and his job name, you try this,

<span t-esc="user.name"/>

<t t-foreach="request.env['hr.employee'].search([('user_id', '=', user.id)])" t-as="obj">

<t t-esc="obj.job_id.name"/>

</t>

Author

it works so nice! thank you so much

Related Posts Replies Views Activity
1
Dec 24
22
1
Nov 24
52
2
Sep 24
11253
2
Nov 21
9237
1
Feb 19
5465