3

How I wanna check if the product_id of a new line added to the mrp.bom.line (Many2many) is equal to a fixed id ('=17'), which may never be added to a BoM. But I can't seem to do this cus he stores the new line as a "NewId", he only writes it to the database when I press the SAVE button. I understood that the NewId is some sort of ID saved in the cache. How can I delete this line when it is equal tot 17?

@api.onchange('bom_line_ids')

def methodA(self):
     list = []
     fixed_list = [17]

     for i in self.bom_line_ids:
         list.append(i.product_id.id)

     for j in list:
        if j in fixed_list:

        HERE DELETE THIS MRP.BOM.LINE AND PRINT WARNING MESSAGE

        warning_message = "Product: " + self.env['product.template'].search([('id','=',j)]).name + " can't be added to the BoM.\n Please select another product."
            return { 'warning': {'title': 'Product error', 'message':warning_message} }
Jesse
  • 727
  • 13
  • 44

1 Answers1

0

if you have fixed id to check then this code many help. and reduce execution time

@api.onchange('bom_line_ids')
def methodA(self):
    fixed_product_id = 17
    is_found = False
    for bom_line in self.bom_line_ids:
        if bom_line.product_id.id == fixed_product_id:
            is_found =True
            bom_line.unlink()

    if is_found:
        warning_message = "Product: " + self.env['product.template'].search([('id','=',fixed_product_id)]) + " can't be added to the BoM.\n Please select another product."
        return { 'warning': {'title': 'Product error', 'message':warning_message} }
Alpesh Valaki
  • 1,611
  • 1
  • 16
  • 36