Tracking existing fields using Odoo Studio

It’s not possible to add tracking to a ‘base’ field without customization. However, it seems that it may be possible to do it using an Automated Action. This is from Ray Carnes on the Odoo Help Forum:

for record in records:
record.message_post(body=env.context['old_values'][record.id])

Here’s a simple way to do it using Odoo Studio. This uses a computed field.

In this example we will track the parent company for an individual contact.

Firstly, add a new text field.

Then (in Developer Mode), click on MORE (in the bottom left hand corner) to display field information.

Tracking: On Change (or ‘1’ in Odoo 14 onwards)

Dependencies: name, parent_id, parent_name

Compute:

for record in self:
     if record['parent_id']:
         record['x_studio_history'] = record.name + ' / Company: ' + record.parent_name

Now any changes are shown in the “chatter” at the bottom of the screen (or the right-hand side on a wide screen):

If it’s working well, we can make the new field invisible on the Form View:

Note: you could do this without Odoo Studio by creating the field in Settings / Technical / (Database Structure) / Fields:

Model is Contact (res.partner)

8 thoughts on “Tracking existing fields using Odoo Studio

    1. Yes. I had to check because I assumed it was already tracked, but only the total amount is NOT tracked!

      Like

      1. OK, I misunderstood. I thought you meant prices on sales orders.

        It should work fine on tracking the sales price on product.template (though I haven’t tried it myself)

        Like

  1. Hi Chris,

    Would a similar concept apply to BOM lines? if a product is changed on a BOM line would it be possible to record the original component -> the new component in the chatter?

    do you know what that code would look like?

    thanks

    Like

  2. This is a great solution!
    Here is another approach that could be used and might be helpful for anyone that wants to enable tracking on many fields in a model.

    # The following code when placed in an Automated Action will provide field tracking
    # in the chatter without requiring a custom module.
    # Making changes to field tracking via a custom module is always preferable.
    # This approach may not be compatible with future versions of Odoo.
    # Considerations for using this approach include:
    # * Databases running on Odoo Online which cannot accept custom modules
    # * When the cost of development is prohibitive for the changes required
    #
    # To ensure correct and improved performance all fields in the tracked_fields list
    # should also be “Watched fields” for this Automated Action
    #
    # An alternative approach can be found here:
    # https://odootricks.tips/about/odoo-studio/tracking-existing-fields-using-odoo-studio/
    # This alternative from Odoo Tips and Tricks requires the creation of a custom field for each field that
    # is to be tracked and it works well for different versions of Odoo

    # Check if tracking is enabled based on context variables
    tracking = not record._context.get(“mail_notrack”) and not record._context.get(“tracking_disable”)

    # If tracking is enabled
    if tracking:
    # Specify the fields to track, if only one field will be tracked,
    # keep the comma after the field name so it is still a valid list
    tracked_fields = (“credit_limit”, “mobile”)

    # Retrieve the old values of the record from the context
    old_vals = record._context.get(‘old_values’, {}).get(record.id, {})

    # Filter out the old values for fields that are not in the tracked fields list
    old_vals_fitlered = dict((key, value) for key, value in old_vals.items() if key in tracked_fields)

    # Filter out tracked fields that don’t have an old value (because they were not updated)
    tracked_fields_with_update = [item for item in tracked_fields if item in old_vals_fitlered]

    # Create a dictionary of initial values for the current record
    initial_values = {record.id: old_vals_fitlered}

    if tracking and initial_values:
    # If Odoo V13.0 use message_track
    # record.message_track(record.fields_get(tracked_fields_with_update), initial_values)

    # If Odoo V16.0 use _message_track
    record._message_track(record.fields_get(tracked_fields_with_update), initial_values)

    Liked by 1 person

    1. Hi Justin – thanks for this, and I finally tested it. My first problem was that copy and paste creates some invalid characters, but having overcome that I found that there’s an error tracking many2one fields. Do you have any idea how to overcome that?

      Like

Leave a comment