-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(lead): reload contact before updating links #29966
Conversation
Contact might have changed since it was created.
They might have changed since they were created. Backport of frappe#29966.
They might have changed since they were created. Backport of frappe#29966.
Codecov Report
@@ Coverage Diff @@
## develop #29966 +/- ##
============================================
+ Coverage 40.68% 59.60% +18.91%
============================================
Files 1083 1109 +26
Lines 68280 69168 +888
============================================
+ Hits 27780 41227 +13447
+ Misses 40500 27941 -12559
|
Can you describe this in a bit more detail? or share what your hook is doing? Hooks shouldn't be triggering Use |
@ankush Lead inserts a Contact at one point in time (and stores a reference to the object). This causes other hooks on Contact to fire, which might modify the contact on the database level. We can avoid the error by reloading the contact before updating it. Produces Errorclass Lead(Document):
def before_insert(self):
self.contact_doc = frappe.get_doc({
"first_name": "Original",
# ...
}).insert()
def after_insert(self):
self.contact_doc.first_name = "I'm Second"
self.save()
class Contact(Document):
def on_update(self):
frappe.db.set_value(self.doctype, self.name, "fist_name", "I'm First") Worksclass Lead(Document):
def before_insert(self):
self.contact_doc = frappe.get_doc({
"first_name": "Original",
# ...
}).insert()
def after_insert(self):
self.contact_doc.reload()
self.contact_doc.first_name = "I'm Second"
self.save()
class Contact(Document):
def on_update(self):
frappe.db.set_value(self.doctype, self.name, "fist_name", "I'm First") |
@barredterra thanks for the explanation. This is very conflicting indeed.
The simplest way to avoid this would be not to set cc: @ruchamahabal / @anupamvs |
@barredterra perhaps it would be better to move to reload right next to insert? class Lead(Document):
def before_insert(self):
self.contact_doc = frappe.get_doc({
"first_name": "Original",
# ...
}).insert()
self.contact_doc.reload()
def after_insert(self):
self.contact_doc.first_name = "I'm Second"
self.save()
class Contact(Document):
def on_update(self):
frappe.db.set_value(self.doctype, self.name, "fist_name", "I'm First") |
Contact might have changed since it was created. In this case the user saw an error message ("contact has been modified") and was unable to save the lead.
The error only shows when you have hooks on Contact that modify Contact.