跳至内容
菜单
此问题已终结
2 回复
1057 查看

Hi All. 

Refer to the picture attached. In CRM i have a custom field "Customer PO" (x_po).

Ones a user has entered the number to the field again this customer. For ex: PO123 against Vodafone Fiji. I don't want any other user to same number for this customer. 


However PO123 can be used against all customers but against same customer it should be only used ones and of anyone enters same number then it should give error upon saving. 


Can someone help me in writing a automated action.



Thanks

Rishal 

形象
丢弃

Perhaps, this module - https://apps.odoo.com/apps/modules/16.0/crm_duplicates/ - will be useful to your goals

编写者

Great Team Thank you all for your feedback. I tried this rule and it worked. 

existing_records = env['crm.lead'].search_count([('x_po', '=', record.x_po) , ('partner_id', '=', record.partner_id.id)])


if existing_records > 1:

raise Warning("PO alreay processed.")



Thanks

最佳答案

Hi,

As you are adding fields from user interface and if you need to add the restriction from the user interface itself, you can use the automated action in odoo and raise warning if the duplicated match is found in the db.

* Install base_automation in db
* from automated action menu under settings menu, create a new automated action for this model
* set type as execute python code
* and add below code

existing_records = env['model_name'].search([('x_po', '=', record.x_po), ('partner_ld', '=', record.partner_id)])

if existing_records > 1:
​raise warning("your warning")

You may have to adjust the code little, but you can follow this.

See an use case here:  https://www.youtube.com/watch?v=8V14lcJlkfA

Thanks

形象
丢弃
编写者

Thank you.

最佳答案

You will try to search from all the records and find x_po and save this in a variable, if the variable have value then try to raise error


@api.constrains('name')

def _check_duplicate_x_po(self):

for record in self:

existing_records = self.search([('name', '=', record.name)])

if len(existing_records) > 1:

raise ValidationError("An existing record with the same X PO value already exists.")


refer this code

形象
丢弃
编写者

Thank you.