Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
163 Vistas

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

Avatar
Descartar
Mejor respuesta

Hi,

Please refer to the code:


if not records:

    raise UserError("No BOMs selected.")


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:

    for line in bom.bom_line_ids:

        if line.product_id == material_to_replace:

            # Use write() instead of direct assignment

            line.write({

                "product_id": replacement_material.id,

            })


Hope it helps.

Avatar
Descartar

You should use assignment and let the ORM manage the database updates.

Mejor respuesta

You cannot import any libraries, UserError is already available.

All record assignments must be in the form:

record['field'] = value
Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
3
ago 25
1124
3
jul 25
655
2
jul 25
1208
1
may 25
934
0
may 25
1096