This question has been flagged
1 Reply
1703 Views


I want to create a new field on the Sale.Order. 

This new field should show:

  • The product of the (first) sale order line WHERE product category = "Advice"


I've tried to make this work based on an earlier question by adding a field to the sale order, but am new to this so need extensive help:

  •  What field type should I create?

  • What should be the object relation?

  • What should I add to computed fields: relation and dependency?

  • What code should I add to select the product of the first sale order line WHERE product category = 'Advice'?

    Hope someone can help!

Avatar
Discard
Best Answer

Hi,

Here first you have to add a field of type Many2one with comodel as product.product has to be added in the sale.order model, then you can define a compute function for the same field which computes the first line from the sale.order.line.

Regarding the field type that has to be added in the sale.order its upto you, if you need to get only the product name you can add a char field also, but better go with the Many2one field.


You can inherit the sale.order model and add field like this,

product_id = fields.Many2one('product.product', 'First Product')


Now you have to define the compute function for the field,

@api.depends('order_line', 'order_line.product_id')
def _compute_advice_product(self):
for rec in self:
advice_pro_lines = rec.order_line.sorted('sequence').filtered(lambda line: line.product_id.categ_id.name == 'Advice')
if advice_pro_lines:
rec.product_id = advice_pro_lines[0].product_id.id

add update the field with compute function,

product_id = fields.Many2one('product.product', 'First Product', compute=_compute_advice_product)


The code is not TESTED, you can check and make necessary adjustments.


Thanks

Avatar
Discard