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

Hi

In Odoo server actions with python code i know the command to update a record (with record.write). But I would like to know the command to create records. What is this command? See below


#Server action with python code on model sale.order,code:

taxId = None


sType = record.opportunity_id.x_studio_type


if sType == 'Renovatie':

    taxId = [87]


if sType == 'Nieuwbouw':

    taxId = [82]


if sType == 'BTW Verlegd':

    taxId = [89]


if taxId != None:

    for record in record.order_line:

        record.write({'tax_id':taxId})


Here I would like to create a sale order line in a sale order. What is the command for this. 

Avatar
Opusti
Best Answer

For creating new records in Odoo using Python code, especially in server actions, you're gonna use the .create() method. This method is pretty straightforward and powerful, allowing you to spawn new records in the database with all the data you specify.

Since you're looking to create a sale order line (sale.order.line) within a sale order (sale.order), you'd typically gather all the necessary field values for a sale.order.line record, then call .create() with those values in a dictionary.
Odoo Beratung Deutschland

Avatar
Opusti
Best Answer

Hi,

Try this command to create sale order line:

record.create({
            'product_id': product_id,
            'product_uom_qty': quantity, 
            'price_unit': price,
            'tax_id': [(6, 0, taxid)], 
        })


Hope it helps

Avatar
Opusti