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

I have  a problem with the time off module I want to create a new time off request for the employee automatically via code but it seems imposible, I tried with this code but I get validation error, the code is:

attendance_list =[]

for employee in all_employees:

​if employee.name not in attendance_employee_list:


​vals = {

​'holiday_status_id': 3,

​'state': 'confirm',

​'user_id': employee.user_id.id,

​'number_of_days': 1,

​'request_date_from': today,

​'request_date_to': today,

​'holiday_type': 'employee',

​}

​self.env['hr.leave'].create(vals)

​record = super(self.env['hr.leave'].create(vals)) 

​attendance_list.append(record)


return attendance_list


and the error is:

Validation error

×

The operation cannot be completed: another model needs the record you want to delete. If possible, archive it.


Model: Time Off (hr.leave), Constraint: hr_leave_employee_id_fkey

Avatar
Discard
Best Answer

Hi Christopher Cervantes

The required field 'employee_id' is missing in the record creation, and there is no need to use 'super' in this function. It's unclear where the 'attendance_list' came from. 


A simpler way to create a leave record based on your code is:

for employee in all_employees: 
if employee.name not in attendance_employee_list:
leave_data = {
'holiday_status_id' : 3 ,
'employee_id' : employee.id ,
'request_date_from' : today ,
'request_date_to' : today ,
'holiday_type' : 'employee' ,
}
new_record = self.env [ 'hr.leave' ] .create ( leave_data )


Hope it helps,
Kiran K



Avatar
Discard
Best Answer

Do you override create method? If so then I don't understand your this code.
Why
"""​self.env['hr.leave'].create(vals)

    ​record = super(self.env['hr.leave'].create(vals))"""
and if create method, why "return attendance_list" ? It's "return record"

Avatar
Discard