Skip to Content
Menú
This question has been flagged
2 Respostes
2337 Vistes

Hello

I am trying to add a server action / contextual action to generate an internal reference for my manufactured products that do not have an internal reference already. The Python code that is executed looks like this:

for record in records:
  if record['route_ids' == 6]:
    if not record['default_code']:
      record['default_code'] = env['ir.sequence'].next_by_code('manufactured_product.default_code')
  else:
    raise UserError(_("Product is not manufactured."))

I've checked that the "Manufacture" route has ID 6. I want Odoo to only allow generation of internal reference for manufactured products. The problem is that if I try to run this server action on products that are not manufactured (only "Buy" route), the action still runs and generates an internal reference.

I'm obviously doing something wrong here. Anyone with a sharp eye who can see what's wrong with my code?

Avatar
Descartar
Best Answer

Hi,

Maybe the issue in your code is with the way you are checking if the product is manufactured or not. The condition if record['route_ids' == 6]: is not correct. You are trying to compare the 'route_ids' field with 6, but it's not the correct way to check if the product has a route with ID 6.

You should use the in operator to check if the route with ID 6 is in the 'route_ids' list. Here's the corrected code:

for record in records:
  if 6 in record['route_ids']:
    if not record['default_code']:
      record['default_code'] = env['ir.sequence'].next_by_code('manufactured_product.default_code')
  else:
    raise UserError(_("Product is not manufactured."))


Hope it helps

Avatar
Descartar
Autor

Thank you for the input! :-)
Unfortunately, it does not seem to work. Now, it will not generate the internal reference with any products. I'm not seeing any errors messages, but then I don't have any shell access so I wouldn't know. But it doesn't look like the conditions are met.

Autor Best Answer

So using ChatGPT I was able to find a way that worked. I think there were some problems accessing the related field in stock.route from the server actions context. Here is the code that actually works:

for record in records:
    try:
        # Use sudo() to execute the operation with elevated privileges
        route_ids = record.sudo().route_ids.ids
       
        if 6 in route_ids:
            if not record.default_code:
                # Use Odoo API to update the record
                record.sudo().write({
                    'default_code': env['ir.sequence'].next_by_code('manufactured_product.default_code')
                })
    except Exception as e:
        # Handle the exception, print or log it as needed
        print(f"Error processing record: {e}")



I've got some concerns as to the security of this code. Any thoughts?


Avatar
Descartar
Related Posts Respostes Vistes Activitat
3
de juny 23
10633
0
de març 15
3542
2
de març 15
6921
1
de març 24
1785
4
d’oct. 23
12477