Skip to Content
Menu
This question has been flagged
2 Replies
1074 Views

Hi there,


I'm creating a button that automatically adds some products to sale order lines using

order.update([   
(0,0,{'product_id':739,'product_uom_qty':70}),
(0,0,{'product_id':742,'product_uom_qty':3})
])

The code works fine, but the products are always added on the second row of sale order lines

Example: https://ibb.co/KGDF89f


I want the new lines to be added after the last line...

How can I 'control' the place where new lines are added?

Avatar
Discard
Best Answer

This happened because of sequence field as the sale order line order by order_id, sequence, id

The default value of the sequence is 10 so when you create it from code it will be 10 so its always added at the first, what you can do is to get the max sequence from all SO lines and then increase it by one and pass it to your cod:

last_so_line = self.env['sale.order.line'].search([('order_id', '=', order.id)], order='sequence desc', limit=1)
last_sequence = last_so_line.sequence + 1 if last_so_line else 10



order.update([
    (0,0,{'product_id':739,'product_uom_qty':70,sequence: last_sequence}),
    (0,0,{'product_id':742,'product_uom_qty':3,sequence: last_sequence+1})
])


Avatar
Discard
Author

Your answer is perfect, thank you!

Best Answer

Hii,
Have you try with adding inverse field in order line ? if not then try with inverse field this might be solve your problem

Avatar
Discard