Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
1055 มุมมอง

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.