콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
Trying to create leave allocation based on a field in hr.employee model.
    @api.onchange('confirm_date')
    def _onchange_confirm_date(self):
        if self.confirm_date:
         
            # Create sick leave allocation
            self.env['hr.leave.allocation'].create({
                'name': 'Automatic Sick Leave Allocation',
                # 'holiday_status_id':
self.env.ref('hr_holidays.holiday_status_sl').id,
                'holiday_status_id': 1,
                'holiday_type': 'employee',
                # 'employee_ids': [(4, self.id)],
                'employee_ids': 20,
                'employee_company_id': 1,
                'department_id': 3,
​'number_of_days': 15.0,
                'state': 'draft',
                'allocation_type': 'regular',
                'date_from': '2024-01-21',
                'date_to': '2024-12-21',
            }) it's giving me the validation error: The employee, department, company or employee category of this request is missing. Please make sure that your user login is linked to an employee.
Here is the sql constraints causing the error:

    _sql_constraints = [
        ('type_value',
         "CHECK( (holiday_type='employee' AND (employee_id IS NOT NULL OR multi_employee IS TRUE)) or "
         "(holiday_type='category' AND category_id IS NOT NULL) or "
         "(holiday_type='department' AND department_id IS NOT NULL) or "         "(holiday_type='company' AND mode_company_id IS NOT NULL))",
         "The employee, department, company or employee category of this request is missing. Please make sure that your user login is linked to an employee."),
        ('duration_check', "CHECK( ( number_of_days > 0 AND allocation_type='regular') or (allocation_type != 'regular'))", "The duration must be greater than 0."),
    ] what am i doing wrong here?
아바타
취소
베스트 답변

Hi,

The error message indicates that the employee, department, company, or employee category is missing from your leave allocation record. This is due to the SQL constraint in the hr. leave. allocation model.

@api.onchange('confirm_date')
def _onchange_confirm_date(self):
if self.confirm_date:
    # Create sick leave allocation
    leave_allocation_data = {
        'name': 'Automatic Sick Leave Allocation',
        'holiday_status_id': 1, 
        'holiday_type': 'employee',
        'employee_ids': [(4, self.id)], 
        'employee_company_id': self.company_id.id
        'department_id': self.department_id.id
        'number_of_days': 15.0,
        'state': 'draft',
        'allocation_type': 'regular',
        'date_from': '2024-01-21',
        'date_to': '2024-12-21',
    }

    self.env['hr.leave.allocation'].create(leave_allocation_data)



Following my assumptions about your HR model structure, I've added the employee, company, and department information in this example. Based on your real model structure and requirements, modify the fields as necessary. To meet the SQL requirements, make sure the values you're entering for each field are legitimate.


Hope it helps

아바타
취소
관련 게시물 답글 화면 활동
2
1월 23
10237
2
8월 25
31705
0
12월 19
4107
1
8월 19
4706
1
10월 18
8741