Creating an automated action in Odoo

 Automated actions are used to trigger some activity based on given conditions. Here we'll be creating an automated action which will be called when a user makes changes to the state of a purchase order.

Note: The module is created for Odoo version 15.0


Steps to follow to create complete module

  • Create an empty addon
  • Create a server action 
  • Create an automated action

 

Create an empty addon

Create a directory named purchase_auto_date. Add en empty __init__.py file and a __manifest__.py  file.

Contents of the manifest file are as follows

 purchase_auto_date/__manifest__.py

# -*- coding: utf-8 -*-
{
'name': "Purchase Base Automation Sample",
'version': '15.0.1.0',
'summary': """Automatic update of notes in purchase""",
'description': """
Purchase Base Automation Sample
-Automatic update of notes in purchase
""",
'author': "Mohammed Amal N",
'category': 'Test',

# any module necessary for this one to work correctly
'depends': ['base_automation', 'purchase'],
'data': [
],
'application': False,
}

App structure as of now:
  • purchase_auto_date
    • __init__.py
    • __manifest__.py

Create a server action

We'll be calling this server action from our automation record, which we will create afterwards. For now, create a new  directory under our addon named data. Inside the directory create a new xml file named data_server_action. Using this file we'll create a new server action record. Contents of the file should looks like this

 purchase_auto_date/data/data_server_action.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="action_purchase_date_update" model="ir.actions.server">
<field name="name">Purchase Order Date Update</field>
<field name="model_id" ref="purchase.model_purchase_order"/>
<field name="state">code</field>
<field name="code">action = record.action_purchase_date_update()</field>
<field name="usage">base_automation</field>
</record>
</data>
</odoo>

I hope fields used in the record are easy to understand without any explanation

Create a method specified in server action for specified model

Create a new directory named models inside our addon to add all models we create

add an __init__.py file to the directory to make it a package

Add a new python file purchase_order to make changes in purchase order model.

 purchase_auto_date/models/purchase_order.py

# -*- coding: utf-8 -*-
import datetime
from odoo import models, fields


class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

state_update_date = fields.Datetime(
string='State Updated On', help='State of the record was updated on')

def action_purchase_date_update(self):
"""Write your automation code in here"""
self.state_update_date = datetime.datetime.now()

Here you might have noticed, we added a new field in purchase order. This is the field we are updating when the automated actions is triggered. We've also added the method action_purchase_date_update to the purchase order model.

Add the new file to init file inside models

purchase_auto_date/models/__init__.py

# -*- coding: utf-8 -*-
from . import purchase_order

 Add models to our module through init

purchase_auto_date/models/__init__.py

# -*- coding: utf-8 -*-
from . import models 

Now lets create a view for the date field we added.

Create a new directory views. Inside the directory create a new xml file purchase_order_views. The file should look like this.

purchase_auto_date/views/purchase_order_views.xml

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="purchase_order_state_update" model="ir.ui.view">
<field name="name">purchase.order.state.update</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<field name="date_order" position="before">
<field name="state_update_date"/>
</field>
</field>
</record>
</odoo>

The above step is not required for making the automated action. But to see our code is running perfectly from UI.

 Add the two xml files we create to the manifest.

 purchase_auto_date/__manifest.py

# -*- coding: utf-8 -*-
{
'name': "Purchase Base Automation Sample",
'version': '15.0.1.0',
'summary': """Automatic update of notes in purchase""",
'description': """
Purchase Base Automation Sample
-Automatic update of notes in purchase
""",
'author': "Mohammed Amal N",
'category': 'Test',

# any module necessary for this one to work correctly
'depends': ['base_automation', 'purchase'],
'data': [
'data/data_server_action.xml',
'views/purchase_order_views.xml'
,
],
'application': False,
}

At this stage your app structure should looks like

  • purchase_auto_date
    • data
      • data_server_action.xml
    • models
      • __init__.py
      • purchase_order.py
    • views
      • purchase_order_views.xml
    • __init__.py
    • __manifest__.py

Create an automated action

Final step is to create a base automation record. This will inform Odoo when to call the server action

Inside the data directory add a new xml file named base_automation

 purchase_auto_date/data/base_automation.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<record id="purchase_date_update_auto" model="base.automation">
<field name="name">Purchase Automated Date</field>
<field name="model_id" ref="purchase.model_purchase_order"/>
<field name="trigger">on_create_or_write</field>
<field name="action_server_id" ref="purchase_auto_date.action_purchase_date_update"/>
<field name="trigger_field_ids"
eval="[(4,ref('purchase.field_purchase_order__state'))]"/>
<field name="active" eval="True"/>
</record>
</data>
</odoo>

Finally add the xml file we created to manifest

# -*- coding: utf-8 -*-
{
'name': "Purchase Base Automation Sample",
'version': '15.0.1.0',
'summary': """Automatic update of notes in purchase""",
'description': """
Purchase Base Automation Sample
-Automatic update of notes in purchase
""",
'author': "Mohammed Amal N",
'category': 'Test',

# any module necessary for this one to work correctly
'depends': ['base_automation', 'purchase'],
'data': [
'data/data_server_action.xml',
'data/base_automation.xml',
'views/purchase_order_views.xml',
],
'application': False,
}

 

Our final app structure should look like

  • purchase_auto_date
    • data
      • base_automation.xml
      • data_server_action.xml
    • models
      • __init__.py
      • purchase_order.py
    • views
      • purchase_order_views.xml
    • __init__.py
    • __manifest__.py 

Now install the app to your Odoo. And you can see "State Updated On" field in purchase order form view. and it's value getting changed whenever you change the state of the purchase order.

The complete code for this project is available on Github

Have a look at it if you get stuck on development


In this blog we only explored running a code from base.automation module. Automation module offers a lot of features like filter based on domain, date based updates etc. Please go through the Odoo documentation to know more. And if you like to explore more from code please go through base automation file

addons/ base_automation/ models/ base_automation.py

Comments

Popular posts from this blog

Odoo Development Tips

PyScript in Odoo