Define an automation rule with XML code

Automation Rule (base.automation)

To my knowledge, there is not a lot of documentation on how to define an Odoo automation rule (formerly referred to as automated action before Odoo version 17) using a data.xml file.  After trial and error, I was able to figure it out.  However, I decided to write this brief blog to make it easier for others that might be struggling. Here is how to define a automation rule (base.automation) on tasks using a data.xml file: 

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="update_task" model="base.automation">
        <field name="name">Update Task</field>
        <field name="model_id" ref="model_project_task"/>
        <field name="trigger">on_time_updated</field>
        <field name="trg_date_range">1</field>
        <field name="trg_date_range_type">minutes</field>
        <field name="trigger_field_ids" eval="[(6, 0, [ref('project.field_project_task__write_date')])]"/>
        <field name="filter_domain">[("sale_order_id.create_project", "=", True)]</field>
        <field name="active" eval="True"/>
        <field name="action_server_ids">update_task_server</field>
    </record>
</odoo>

As you can see, I set the automation rule to run every 1 minute using this code.

<field name="trigger">on_time_updated</field>
<field name="trg_date_range">1</field>
<field name="trg_date_range_type">minutes</field>
<field name="trigger_field_ids" eval="[(6, 0, [ref('project.field_project_task__write_date')])]"/>

If you do not define the trigger_field_ids, the system will throw an error.  I solved this by setting this as project_task write date.  I was able to get the external id for this field by going to Settings --> Technical --> Fields, navigating to the write date field, and then clicking View Metadata.  

Previously, the trigger for this action was set to anytime the record was saved (on_create_or_write) rather than a timed condition.  I changed this to a timed condition because I realized that the system was less performant when the action was set to on_create_or_write.  This may not be the case for your code, but mine was hitting an external API which caused the task to lag for a few seconds after saving the task.  If you are not calling an external API, this may not be the case.  ​Furthermore, it is important to note that the triggers on_create and on_write will be deprecated by Odoo per comments that are present in the base_automation.py file upon writing this​.​​ 

​​

The automation rule should reference a server action to actually execute.  I have defined the associated server action here:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="update_task_server" model="ir.actions.server">
        <field name="name">Execute Python Code</field>
        <field name="model_id" ref="model_project_task"/>
        <field name="state">code</field>
        <field name="code">
            model.update_task(record)
        </field>
        <field name="usage">base_automation</field>
        <field name="base_automation_id" ref="update_task"/>
    </record>
</odoo>

As you can see, the code that I have defined is located in an external python file.  However, you can define the code directly from the xml file if you so choose.  It is much cleaner to create a function on the target model and reference the function in the xml file.

Hopefully, you found this helpful!

Define an automation rule with XML code
Jacob Neubaum February 22, 2024
Share this post
Tags
Archive
Sign in to leave a comment