Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
352 Prikazi

I get a studio automation on sale.order, which is below.


p6120 and p6121 are find but when trying to create the order line, Odoo can't do it because :

" (Enregistrement : product.product(6120,), Utilisateur : 2)"


What is the issue and the solution :-) ? 


Thanks in advance

if not env.context.get('skip_auto_add_6120'):

try:
    p6120 = env.ref('__custom__.6120')
except:
    p6120 = None
try:
    p6121 = env.ref('__custom__.6121')
except:
    p6121 = None

# Si le produit cible (6120) n'existe pas on ne fait rien
if p6120:
    
    ids_to_check = [p6120.id] + ([p6121.id] if p6121 else [])
   
       # S'il n'y a aucune ligne avec 6120 ni 6121, on ajoute 6120
    if not any(line.product_id.id in ids_to_check for line in record.order_line):
        #raise UserError("La valeur de la variable est : %s" % p6120.id)
        record.with_context(skip_auto_add_6120=True).order_line.create({
            'order_id': record.id,
            'product_id': p6120.id,
            'product_uom_qty': 1,
            'product_uom': p6120.uom_id.id,
            'price_unit': p6120.list_price,
        })
Avatar
Opusti
Best Answer

Hi,


Use env['sale.order.line'].create() instead. This is the proper way to create records in Odoo ORM.


Try the following code:


if not env.context.get('skip_auto_add_6120'):

    try:

        p6120 = env.ref('__custom__.6120')

    except:

        p6120 = None

    try:

        p6121 = env.ref('__custom__.6121')

    except:

        p6121 = None


    if p6120:

        ids_to_check = [p6120.id] + ([p6121.id] if p6121 else [])

       

        if not any(line.product_id.id in ids_to_check for line in record.order_line):

            env['sale.order.line'].with_context(skip_auto_add_6120=True).create({

                'order_id': record.id,

                'product_id': p6120.id,

                'product_uom_qty': 1,

                'product_uom': p6120.uom_id.id,

                'price_unit': p6120.list_price,

            })


Hope it helps

Avatar
Opusti
Avtor

Hi,

thanks for your reply. Unfortunately, the error is still the same "Oh mince !
Enregistrement inexistant ou supprimé.
(Enregistrement : product.product(6120,), Utilisateur : 2)"

By the way, This product is really existing.

The error comes here : 'product_id': p6120.id,

something like this (to give the p6120 record) does not work too : product = env['product.product'].browse(6120) for information.

Any other idea?

Related Posts Odgovori Prikazi Aktivnost
0
sep. 25
34
0
sep. 25
7
2
sep. 25
233
1
sep. 25
572
2
avg. 25
706