Basically I was adding 2 fields to the form view: Material to replace and Replacement material, then I would give the option to select multiple BOMs and each of them will replace the material selected with the replacement material. My problem was when I tried adding the code, It gives me forbidden opcode(s) error.
Im using odoo studio btw.
This is the code:
from odoo.exceptions import UserError
# Get the material to replace and the replacement material from the FIRST selected record
if records:
first_record = records[0]
material_to_replace = first_record.x_studio_material_to_replace_1
replacement_material = first_record.x_studio_replacement_material_1
if not material_to_replace or not replacement_material:
raise UserError("Please specify both 'Material to Replace' and 'Replacement Material' in the first selected BOM.")
# Loop through the selected BOMs
for bom in records:
# Loop through the BOM lines
for line in bom.bom_line_ids:
# Check if the material matches the one to replace
if line.product_id == material_to_replace:
# Replace the material
line.product_id = replacement_material
line.product_qty = line.product_qty #This line is added to force the recomputation of values
else:
raise UserError("No BOMs selected.")
And this is the error: forbidden opcode(s) in 'from odoo.exceptions import UserError\n\n# Get the material to replace and the replacement material from the FIRST selected record\nif records:\n first_record = records[0]\n material_to_replace = first_record.x_material_to_replace\n replacement_material = first_record.x_replacement_material\n\n if not material_to_replace or not replacement_material:\n raise UserError("Please specify both \'Material to Replace\' and \'Replacement Material\' in the first selected BOM.")\n\n # Loop through the selected BOMs\n for bom in records:\n # Loop through the BOM lines\n for line in bom.bom_line_ids:\n # Check if the material matches the one to replace\n if line.product_id == material_to_replace:\n # Replace the material\n line.product_id = replacement_material\n line.product_qty = line.product_qty #This line is added to force the recomputation of values\nelse:\n raise UserError("No BOMs selected.")': IMPORT_NAME, IMPORT_FROM, STORE_ATTR