Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
552 Vues

I am using odoo studio to customize a fuel sales app. i have created two fields: one for opening meter reading and the other for closing meter reading corresponding to a date. When I create a new record, I need the previous opening meter reading to auto fill the new opening meter reading.

Can you help please?

Avatar
Ignorer
Meilleure réponse

Hi,

Please refer to the blog below to set the compute function for the field.https://www.cybrosys.com/blog/how-to-set-compute-function-for-field-using-odoo-17-studio.

Steps to Auto-Fill opening_meter_reading in Odoo Studio

Set opening_meter_reading as a Computed Field:

Open the properties of the opening_meter_reading field.

Enable the "Computed" option to make the field computed.

Define the Compute Method:

In the "Python Code" section for the opening_meter_reading field, you can add the following code:

for record in self:

          previous_record = self.search([], order='your_date_field desc', limit=1)

          if previous_record:

              record.opening_meter_reading = previous_record.closing_meter_reading

          else:

              record.opening_meter_reading = 0 # or some default value


Replace your_date_field with the actual date field used in your model



Hope it helps

Avatar
Ignorer