Search code examples
pythonodooodoo-13

How to programmatically create a sale order line (Odoo 13)


I have a sales record

models.execute_kw(db, uid, password, 'sale.order', 'create', [{
         
            'partner_id': 10,
            'order_line': [(0, 0, {'product_id':1,'product_uom_qty':2}),

        

I need to be able to create 'order_line' programaticaly based on a number from a variable. For example if variable = 3

  models.execute_kw(db, uid, password, 'sale.order', 'create', [{

         
            'partner_id': 10,
            'order_line': [(0, 0, {'product_id':1,'product_uom_qty':2}),
            'order_line': [(0, 0, {'product_id':1,'product_uom_qty':2}),
            'order_line': [(0, 0, {'product_id':1,'product_uom_qty':2}),

I followed the exact steps from here, https://www.odoo.com/forum/help-1/question/programmatically-create-a-sale-order-line-99981. But I keep getting errors as it seems Odoo has changed quite a bit since 2016. What would be the best way to go about this?


Solution

  • In your second example, you forgot the closing bracket when you provide the value of the order line, and when you write order_line three times, it is equivalent to provide only the last value.

    If you need to create three lines, you need to pass three tuples in a list using the special commands.

    The following example will create a sale order with three identical lines:

    models.execute_kw(DB, uid, PASSWORD, 'sale.order', 'create', [{
    
                'partner_id': 10,
                'order_line': [(0, 0, {'product_id':1,'product_uom_qty':2}),
                               (0, 0, {'product_id':1,'product_uom_qty':2}),
                               (0, 0, {'product_id':1,'product_uom_qty':2})
                              ]
                }])