2

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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
apexprogramming
  • 403
  • 2
  • 14
  • 1
    2 things. The dictionary must have unique keys. Use a for-loop to execute the same command multiple times. Also what is the error? – drum Jul 28 '20 at 21:18
  • In how to write/create one2many entries Odoo hasn't changed since a lot of years. Just for information ;-) – CZoellner Jul 29 '20 at 09:30

1 Answers1

2

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})
                          ]
            }])
Kenly
  • 24,317
  • 7
  • 44
  • 60