Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
2592 Weergaven

Hi all,

I am attempting to add in some automation in v15 for the sales order function, specifically to prevent duplication of the client reference number (field client_order_ref). 


I have already found the automation section and added the model, trigger and trigger fields (the client ref) but I am stuck on what python code to use to search for duplicates and then show a warning to stop the user from proceeding if it already exists.


Can anyone assist? Thanks in advance!


Apologies I cannot provide a screenshot as I don't have enough karma yet. 

Avatar
Annuleer
Auteur Beste antwoord

Hi Geekboy (great name btw),


I tried using the above code but my DB doesn't recognise the inherit field in the 4th line. Any other suggestions? 

Avatar
Annuleer
Beste antwoord

To prevent duplication of the client_order_ref field in the sales order model in Odoo 15, you can use a @api.constrains decorator to add a constraint that checks if the value already exists in the database. If a duplicate value is found, you can raise a ValidationError to prevent the user from proceeding with the sales order creation.

Here's an example code:

python

from odoo import api, fields, models, _

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

​client_order_ref = fields.Char('Client Reference')

​# Constraint to prevent duplication of client_order_ref
​@api.constrains('client_order_ref')
​def check_duplicate_client_order_ref(self):
​for order in self:
​if order.client_order_ref:
​duplicate_order = self.search([('client_order_ref', '=', order.client_order_ref), ('id', '!=', order.id)], limit=1)
​if duplicate_order:
​raise ValidationError(_('The client reference number already exists in the system. Please enter a unique value.'))

In this code snippet, the SaleOrder model inherits the sale.order model and adds a new client_order_ref field. The @api.constrains decorator is used to define a constraint function that checks for duplicates of the client_order_ref field in the database.

The for loop iterates over each sale.order record and checks if the client_order_ref field is not empty. The search() function is used to search for other sale.order records that have the same client_order_ref value, excluding the current record (order.id). If a duplicate record is found, a ValidationError is raised with a warning message that informs the user that the client reference number already exists in the system.

Once you have added this code to your Odoo 15 instance, the constraint will be automatically applied whenever a new sales order is created or an existing one is updated. If the user tries to save a sales order with a duplicate client_order_ref value, they will be prevented from proceeding and will be shown the warning message you defined in the ValidationError

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
2
jul. 23
2298
1
mei 23
1554
1
feb. 25
2769
2
jun. 24
1037
0
feb. 24
640