Open employee card from button Odoo 14

Viewed 42

I have a tree view with certain custom fields from employees, in this tree view I have included a button in each line and I pretend that clicking on that button, should open the employee card for the selected employee.

My code is as follows:

Tree view:

<record model="ir.ui.view" id="informes_informe_empleados_tree">
<field name="name">informes.informe_empleados.tree</field>
<field name="model">informes.informe_empleados</field>
<field name="arch" type="xml">
    <tree>
        <button name="open_form_empleados" string="Abrir ficha" type="object" class="oe_highlight"/>
        <field name="name"/>
        <field name="employee_type_principal"/>
        <field name="begin_date"/>
        <field name="end_date"/>
    </tree>
</field>
</record>

And the function called by the button:

def open_form_empleados(self):
    return {
        'type': 'ir.actions.act_window',
        'view_type': 'form',
        'res_id': "I don't know what to put here",
        'view_mode': 'form',
        'res_model': 'hr.employee',
        'view_id': self.env.ref('per_employee_model.hr_employee_inherit').id,
        'target': 'new'
    }

I have made some research and I think that I have to use res_id to indicate the employee id but I didn't manage to find the correct syntax to make it work.

I would appreciate any hint. Thanks in advance.

3 Answers

When you click on the Abrir ficha button, self will be a recordset of the informes.informe_empleados model corresponding to the selected line

If you have the employee reference (let's say employee_id), you can simply use self.employee_id.id.

If you don't have an employee reference and the custom fields from employees can identify a specific employee, you can use the search method to get the employee Id.

To open the emloyee form view for a specific employee, you need to set the res_id to the employee ID.

You can try to give a context to the button like so:

<button name="open_form_empleados" string="Abrir ficha"
type="object" class="oe_highlight" context="{'some_name_id': hr_employee_id}"/>

You need to have the 'hr_employee_id' in the tree view.

And get it back in python like this:

self.env.context.get('some_name_id')
return {
    'type': 'ir.actions.act_window',
    'view_type': 'form',
    'res_id': self.env.context.get('some_name_id'),
    'view_mode': 'form',
    'res_model': 'hr.employee',
    'view_id': self.env.ref('per_employee_model.hr_employee_inherit').id,
    'target': 'new'
}

Regards,

finally it worked adding: 'res_id': self.id

Thanks

Related