From 24fe539ff258774970087b1ef6383478bacf8a61 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 1 Feb 2024 10:44:00 +0530 Subject: [PATCH] fix: Retry contact update if it fails due to conflict (#24654) --- frappe/core/doctype/user/user.py | 55 +++++++++++++++++--------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/frappe/core/doctype/user/user.py b/frappe/core/doctype/user/user.py index fb0362c749..971ce3e066 100644 --- a/frappe/core/doctype/user/user.py +++ b/frappe/core/doctype/user/user.py @@ -1313,34 +1313,37 @@ def create_contact(user, ignore_links=False, ignore_mandatory=False): except frappe.DuplicateEntryError: pass else: - contact = frappe.get_doc("Contact", contact_name) - contact.first_name = user.first_name - contact.last_name = user.last_name - contact.gender = user.gender - - # Add mobile number if phone does not exists in contact - if user.phone and not any(new_contact.phone == user.phone for new_contact in contact.phone_nos): - # Set primary phone if there is no primary phone number - contact.add_phone( - user.phone, - is_primary_phone=not any( - new_contact.is_primary_phone == 1 for new_contact in contact.phone_nos - ), - ) + try: + contact = frappe.get_doc("Contact", contact_name) + contact.first_name = user.first_name + contact.last_name = user.last_name + contact.gender = user.gender + + # Add mobile number if phone does not exists in contact + if user.phone and not any(new_contact.phone == user.phone for new_contact in contact.phone_nos): + # Set primary phone if there is no primary phone number + contact.add_phone( + user.phone, + is_primary_phone=not any( + new_contact.is_primary_phone == 1 for new_contact in contact.phone_nos + ), + ) - # Add mobile number if mobile does not exists in contact - if user.mobile_no and not any( - new_contact.phone == user.mobile_no for new_contact in contact.phone_nos - ): - # Set primary mobile if there is no primary mobile number - contact.add_phone( - user.mobile_no, - is_primary_mobile_no=not any( - new_contact.is_primary_mobile_no == 1 for new_contact in contact.phone_nos - ), - ) + # Add mobile number if mobile does not exists in contact + if user.mobile_no and not any( + new_contact.phone == user.mobile_no for new_contact in contact.phone_nos + ): + # Set primary mobile if there is no primary mobile number + contact.add_phone( + user.mobile_no, + is_primary_mobile_no=not any( + new_contact.is_primary_mobile_no == 1 for new_contact in contact.phone_nos + ), + ) - contact.save(ignore_permissions=True) + contact.save(ignore_permissions=True) + except frappe.TimestampMismatchError: + raise frappe.RetryBackgroundJobError def get_restricted_ip_list(user): -- GitLab