Hello,
any idea how to show an error when a product exists in Sales order line?
I tried to do this but nothing happens.
@api.model
def check_exist_product(self):
err_products = []
for line in self.line_ids:
if line\.product_id\.id\ not\ in\ err_products:
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ err_products\.append\(line.product_id.id)
else:
raise exceptions.Warning(_('Product Exist'))
You can override create method and check if product already exists or not...
Use the UserError or ValidationError (or what fits your needs the best) to generate a Popup on the users side. The timing when to show it should be used (as from @sehrisch proposed) on the create method of the sale.order.line (extend it here to raise the exception and stop the creation process also, to prevent it.
Another one could be to use the _sql_constraints definition in the model to define a "uniqueness" per product_id and the order_id. So odoo would automatically throw exceptions for you if someone tries to insert the same product.id into a sale.order
raise UserError(
_(
"Product %s alredy in the Sale Order"
) % (product.name)
)