Skip to Content
Menu
This question has been flagged

Hi,

I need to set change some values in one2many from js dynamically, And also need to add a new line from js to one2many. The values for one2many field will be saved in the browser storage . So need to fetch the data from the session storage and need to do the changes in the one2many without using the RPC. 

Avatar
Discard
Best Answer

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.

Avatar
Discard

How to call below in V15?:

const fieldWidget = this.field_manager.fields.my_one2many_field;

fieldWidget.update_values(values);

Also how to set selected field on one2many filed to sessionStorage before getting it from sessionStorage, tried with below code but not working?

sessionStorage.setItem('soc_budget', JSON.stringify({
targetType: $(event.currentTarget).find('tr.o_data_row').data('id'),
}));

Related Posts Replies Views Activity
1
Jun 15
7992
2
Jan 23
3658
0
Feb 21
2476
1
Aug 23
411
1
Sep 21
19276