Automated Action to set Routes

The requirement is to automatically set one or more Routes for a new product. It’s easy for users to forget and setup products incorrectly, and that can cause problems, particularly for companies that operate in a “Make to Order” environment.

The screen looks like this:

This is a many2many relationship:

  • it uses the many2many_checkboxes widget
  • the only routes shown here are the ones that are enabled as “applicable on product”.
    • Domain: [["product_selectable","=",True]]

One simple and obvious way to handle this is by setting routes on Product Categories. When a user selects a Product Category, the Routes will be copied to the product.

However, that may not always be a good solution (maybe you want to use Product Categories for something else).

Instead we can use Automated Actions, but as this is a many2many field (multiple values can be selected) it is a little more complex.

We need to know which routes can be selected and what are their Record IDs. Odoo Studio makes it simple to do that, but the standard download tool is another option.

As explained above, only the first two Routes are available for use on products. The last four apply to warehouses.

Now we have all the information we need, so we can enable Developer Mode and navigate to Settings / Technical / (Automation) / Automated Actions:

  • Model = Product Template
  • Action To Do = Execute Python Code
  • Trigger Condition = On Creation
  • Apply On:
    • Use domain to select which products are updated. In this example, we’ll only update stockable / storable products, though you might want to include consumables as well.

Python Code

In this example, we are selecting Buy (5) and Make to Order (1). Multiple routes are selected by entering the Records IDs separated by commas.

record['route_ids'] = [5,1]

It would be simple to extend this if there different types of products which require different Routes, though in most cases Product Categories should be a good enough solution.

Server Action

This could also be set as a Server Action to easily update existing products. The Python code would need to be slightly changed:

for record in records:
     record['route_ids'] = [5,1]

It can be enabled as a “Contextual Action” so that it is available from the Action dropdown (more information here)

User-defined defaults

Note: it’s also possible to do this with user-defined defaults, though not in the normal way using the debug menu. Credit to Michael Rutherford who suggested this on the Odoo Help Forum.

Enable Developer Mode and navigate to Settings / Technical / (Actions) / User-defined defaults and click on “Create”.

Field is Routes on the Product Template.

The “Default Value (JSON format)” is the same as the Python code above

5 thoughts on “Automated Action to set Routes

  1. I tested it in Odoo 12 and it works for consumables. Domain = [[“type”,”=”,”consu”]]

    If you can share a link to a screenshot I’ll take a look.

    Like

  2. Hi, in purchase order Odoo picks one of the value from drop down in “Deliver To” field, my requirement is to set as empty on creation so that user deliberately select data in this required field, can this be done by any means some how (without customization) ? like automated/server action, default values etc. ?

    Like

Leave a comment