Skip to Content
Menu
This question has been flagged
1 Reply
3428 Views

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'))

Avatar
Discard

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)
)

Best Answer

Hello 

Try below code may work

@api.constrains('product_id')
def check_exist_product(self):
err_products = []
for line in self:
line_ids = self.search[('product_id', '=', line.product_id.ud),('id', '!=', line.id)])
if line_ids:
raise exceptions.Warning(_('Product Exist'))


Avatar
Discard
Related Posts Replies Views Activity
0
Sep 24
120
3
Aug 24
515
0
Feb 24
263
0
Feb 24
155
1
Jan 24
1315