Create analytic accounts for sales orders using Automated Actions

This is similar to creating a BOM but adds some extra options (kinda). The requirement is to create an Analytic Account for each Sales Order – and use it on the order. This replicates the standard functionality in Odoo to automatically create an Analytic Account for each project.

Enable Developer Mode and navigate to Settings / Technical / (Automation) / Automated Actions:

  • Model = Sale Order                                            
  • Action To Do = Create a new Record
  • Trigger Condition = On Creation       
  • Create/Write Target Model = Analytic Account
  • Link using field = Analytic Account (sale.order)   
    • analytic_account_id                        

Data to Write

RecordEvaluation TypeValue
Analytic Account (account.analytic.account)Python expressionrecord.name
Customer (account.analytic.account)Python expressionrecord.partner_id.id

Problem

Unfortunately there’s a problem with this. It isn’t possible to enter the “Link Using Field” because the domain is incorrect in the XML. It’s a bug and it has been logged in GitHub.

The problem is in the Form View for Server Action (base.view_server_action_form). This is the domain (from Odoo 12):

domain="[('model_id', '=', model_id),
         ('relation', '=', crud_model_name),
         ('ttype', 'in', ['many2one'])]"

  1. The model_id comparison is correct
  2. The crud_model_name comparison is wrong
    • It could be removed (which would allow selection of fields that do not link to the correct Model)
    • Or it can be fixed using Odoo Studio, as explained here
    • Or you could use the download / upload tool (Model ir.actions.server) to enter the correct value.
  3. In Odoo 13 the type has been expanded to include one2many and many2many, but that is not the issue here (and if line 2 was correct this would be redundant).

Another technique

This is another way to create a record in another Model (but doesn’t solve the linking problem).

  • Model = Sale Order                                            
  • Action To Do = Execute Python Code
  • Trigger Condition = On Creation                                       

Python Code

env['account.analytic.account'].create({
         'name':record.name,
         'partner_id':record.partner_id.id
})

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s