To update values in a one2many field in Odoo from JavaScript, you can use the update_values method on the one2many field widget instance. This method takes an array of objects with the id and value properties, where id is the id of the record in the one2many field and value is the new value for that record.
Here is an example of how you can use this method to update values in a one2many field:
// Get the one2many field widget instance
const fieldWidget = this.field_manager.fields.my_one2many_field;
// Fetch the data from the session storage
const data = JSON.parse(sessionStorage.getItem('my_one2many_field_data'));
// Map the data to an array of objects with the required format for the `update_values` method
const values = data.map(item => ({
id: item.id,
value: item.value,
}));
// Use the `update_values` method to update the values in the one2many field
fieldWidget.update_values(values);
To add a new line to a one2many field from JavaScript, you can use the add_record method on the one2many field widget instance. This method takes an object with the value property, where value is the value for the new record.
Here is an example of how you can use this method to add a new line to a one2many field:
Copy code// Get the one2many field widget instance
const fieldWidget = this.field_manager.fields.my_one2many_field;
// Fetch the value for the new record from the session storage
const value = JSON.parse(sessionStorage.getItem('my_one2many_field_new_record'));
// Use the `add_record` method to add a new line to the one2many field
fieldWidget.add_record({ value });
Keep in mind that these changes will only be visible in the frontend and will not be saved in the database unless you explicitly call the save method on the form view.