Search code examples
odoo-8odoo

Product adds to line when field changes


I have button that makes field "urgent" true. So when i push that button i need that product "services" would be added to sale.order.line. Tried to make it this way but with no luck.

class SaleOrder(models.Model):
    _inherit = "sale.order"

    urgent = fields.Boolean('Urgent')

    @api.multi
    def urgent_activate(self):
        self.urgent = True

    @api.onchange('urgent')
    def urgent_onchange(self):
        if self.urgent:
            vals = {
                'name': 'test',
                'product_id': 1
            }
        self.order_line.write(vals)

Solution

  • You can try with following code:

    @api.multi
    def urgent_activate(self):
        self.urgent = True
        vals = {
            'name': 'test',
            'product_id': 1,
            'order_id':self.id
        }
        self.order_line.create(vals)
    

    About your comment... maybe something like this

    @api.multi
    def urgent_activate(self):
        self.urgent = True
    
        for order_line in self.order_line:
            if order_line.name == 'Services':               
                vals = {
                    'name': 'test',
                    'product_id': 1,
                    'order_id':self.id
                }
                self.order_line.write(order_line.id,vals)   
    

    I dont test this second part, let me know if works