Search code examples
odooodoo-13

How to reference a planning type in a plan


I have a custom odoo module, which extends some existing modules like hr. I want to create an onboarding plan with several predefined tasks in it.

This is my plan acitivity type xml which works at it should. If I update the applikation with this file, I get the desired tasks in the planning types overview.

    <?xml version="1.0" encoding="utf-8"?>
<odoo>

    <record id="hr_plan_activity_type_create_work_contract" model="hr.plan.activity.type">
        <field name="activity_type_id" ref="mail.mail_activity_data_todo"/>
        <field name="responsible">manager</field>
        <field name="summary">Create work contract</field>
        <field name="note">Create the work contract for the employee.</field>
    </record>

    <record id="hr_plan_activity_type_employee_model_in_erp" model="hr.plan.activity.type">
        <field name="activity_type_id" ref="mail.mail_activity_data_todo"/>
        <field name="responsible">manager</field>
        <field name="summary">Employee model in ERP</field>
        <field name="note">Complete the employee model in ERP (AHV, Banking, etc.)</field>
    </record>

</odoo>

This is my plan.xml which should create a plan with the activity types. The creation of the plan works, but if I reference the activity types, I'll get an error message.

<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <!-- Onboarding -->

    <record id="hr_plan_onboarding" model="hr.plan">
        <field name="name">Onboarding</field>
        
    <field name="plan_activity_type_ids"
           eval="[(6,0,[ref('mycompany.hr_plan_activity_type_employee_model_in_erp')])]"/>

    <field name="plan_activity_type_ids"
           eval="[(4,0,[ref('mycompany.hr_plan_activity_type_create_work_contract')])]"/>
    </record>

</odoo>

In the manifest.py file I first load the plan.activity.type.xml and then the plan.xml so this shouldn't be a problem.

This is the error message I get when I try to upgrade my customized module mycompany:

File "C:\Program Files (x86)\Odoo 13.0e\server\odoo\addons\base\models\ir_model.py", line 1670, in xmlid_lookup
    raise ValueError('External ID not found in the system: %s' % xmlid)
odoo.tools.convert.ParseError: "External ID not found in the system: hr.plan.activity.type.hr_plan_activity_type_create_work_contract" while parsing file:/c:/users/myuser/appdata/local/openerp%20s.a/odoo/addons/13.0/mycompany/data/hr/plan.xml:2, near
<odoo>

    <!-- Onboarding -->

    <record id="hr_plan_onboarding" model="hr.plan">
        <field name="name">Onboarding</field>
        <field name="plan_activity_type_ids" ref="hr.plan.activity.type.hr_plan_activity_type_create_work_contract"/>
    </record>
 

Does anyone have any ideas?


Solution

  • String identifier stored in ir.model.data, can be used to refer to a record regardless of its database identifier during data imports or export/import roundtrips.

    External identifiers are in the form module.id (e.g. account.invoice_graph). From within a module, the module. prefix can be left out.

    Sometimes referred to as xml id or xml_id as XML-based Data Files make extensive use of them.

    In your example you used model_name.id which probably does not exist in the database, to reference hr_plan_activity_type_create_work_contract record you just need to replace the model name with the module name.

    I can see from the log message that the module name is mycompany, try to replace the model name with mycompany:

    <record id="hr_plan_onboarding" model="hr.plan">
        <field name="name">Onboarding</field>
        <field name="plan_activity_type_ids" ref="mycompany.hr_plan_activity_type_create_work_contract"/>
    </record>
    

    Update:plan_activity_type_ids is an x2many field

    Use the special commands format to set the x2many field values:

    <record id="hr_plan_onboarding" model="hr.plan">
        <field name="name">Onboarding</field>
        <field name="plan_activity_type_ids" eval="[(6,0,[ref('mycompany.hr_plan_activity_type_create_work_contract')])]"/>
    </record>
    

    Edit: Only the first one shows up in the GUI

    To replaces all existing records in the set by the ids list (using '(6, 0, ids)') you can provide a list of ids inside the triplet. You can find an example in res_partner_demo.xml inside the base module.

    Example:

    <field name="plan_activity_type_ids" eval="[(6,0,[ref('mycompany.hr_plan_activity_type_employee_model_in_erp'), ref('mycompany.hr_plan_activity_type_create_work_contract')])]"/>
    

    To add an existing record of id id to the set (using (4, id)) you need to provide one id for each triplet. You can find an example in base_groups.xml inside the base module.

    Example:

    <field name="plan_activity_type_ids" eval="[(4,ref('mycompany.hr_plan_activity_type_employee_model_in_erp')), (4,ref('mycompany.hr_plan_activity_type_create_work_contract'))]"/>