[FIX] survey_record_generation: link to already created record at creation
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m36s

A NOT required "other_record" field was always set after the loop with a
write(), even when the linked record was already created. This late write
triggers the recomputation of stored computed fields depending on the
relation, overriding the values answered by the user.

E.g. with subscription_oca, sale.subscription.line price_unit depends on
sale_subscription_id.partner_id/pricelist_id: a free amount typed in a
numerical_box question (33) was replaced by the product price (20) when
the line was linked to its subscription.

Now the relation is given to create() when the linked record already
exists, so that the user's values are protected from recomputation. The
deferred write is kept for records created later.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 12:09:36 +02:00
parent dfaaa9405c
commit 500f14a57a
3 changed files with 85 additions and 26 deletions

View File

@@ -11,7 +11,7 @@ Allow to create record of any model when sending the form :
* Associate question with fields
* For x2m fields : Associate values to questions
""",
"version": "18.0.1.0.1",
"version": "18.0.1.0.2",
"license": "AGPL-3",
"author": "Elabore",
"website": "https://www.elabore.coop",

View File

@@ -160,8 +160,9 @@ class SurveyUserInput(models.Model):
elif field_value.value_origin == "question":
value = self.get_value_from_user_answer(field_value, user_input)
elif field_value.value_origin == "other_record":
# if the other_record value is a required field, get it or raise
value = self.get_required_value_from_other_record(
# if the other record is already created, get it
# (or raise if it is not created yet and the field is required)
value = self.get_value_from_other_record(
model, created_records, field_value
)
# otherwise, we update the record later (out of this for loop)
@@ -198,35 +199,30 @@ class SurveyUserInput(models.Model):
duplicate = self.env[model].search(uniq_domain, limit=1)
return duplicate
def get_required_value_from_other_record(
def get_value_from_other_record(
self,
model: str,
created_records: dict[Any, Any],
field_value: "SurveyRecordCreationFieldValues",
) -> Any:
model_class = self.env[model]
if model_class._fields[field_value.field_id.name].required:
# check if the other record is already created,
# if yes add it to vals, else raise
if (
len(created_records) > 0
and created_records[field_value.other_created_record_id.id]
):
linked_record = created_records[field_value.other_created_record_id.id]
return self.get_value_for_relational_field(field_value, linked_record)
else:
raise UserError(
_(
"The field %(field)s is mandatory for model %(model)s. "
"In Record Creation tab, drag %(record)s "
"on top of the model %(model)s."
)
% {
"field": field_value.field_id.display_name,
"model": model,
"record": field_value.other_created_record_id.name,
}
linked_record = created_records.get(field_value.other_created_record_id.id)
if linked_record:
# The other record is already created: link it right away.
return self.get_value_for_relational_field(field_value, linked_record)
if self.env[model]._fields[field_value.field_id.name].required:
raise UserError(
_(
"The field %(field)s is mandatory for model %(model)s. "
"In Record Creation tab, drag %(record)s "
"on top of the model %(model)s."
)
% {
"field": field_value.field_id.display_name,
"model": model,
"record": field_value.other_created_record_id.name,
}
)
return None
def get_value_from_user_answer(
self,

View File

@@ -1,3 +1,5 @@
from unittest.mock import patch
from psycopg2 import IntegrityError
from odoo.addons.survey.tests.common import SurveyCase
@@ -1072,3 +1074,64 @@ class TestSurveyRecordCreation(SurveyCase):
self.assertEqual(self.answer.partner_id, jean)
self.assertNotEqual(self.answer.partner_id, jeanne)
self.assertEqual(self.answer.email, jean.email)
def test_link_to_already_created_record_is_set_at_creation(self):
# When a NOT required "other_record" field points to a record that
# is already created (lower sequence), the link must be given in
# create() and not written afterwards: a later write would trigger
# computed fields depending on this link (e.g. price_unit of a
# sale.subscription.line depends on its subscription) and override
# the values answered by the user.
self.survey_record_creation.sequence = 0
child_creation = self.env["survey.record.creation"].create(
{
"name": "Child contact",
"survey_id": self.survey.id,
"model_id": self.res_partner_model.id,
"sequence": 1,
}
)
self.env["survey.record.creation.field.values"].create(
{
"survey_record_creation_id": child_creation.id,
"survey_id": self.survey.id,
"model_id": self.res_partner_model.id,
"field_id": self.name_field.id,
"value_origin": "fixed",
"fixed_value_char": "Child",
}
)
parent_field = self.env["ir.model.fields"]._get("res.partner", "parent_id")
self.env["survey.record.creation.field.values"].create(
{
"survey_record_creation_id": child_creation.id,
"survey_id": self.survey.id,
"model_id": self.res_partner_model.id,
"field_id": parent_field.id,
"value_origin": "other_record",
"other_created_record_id": self.survey_record_creation.id,
}
)
self.answer = self._add_answer(
survey=self.survey, partner=False, email="jean@test.fr"
)
self._add_answer_line(
question=self.question_name, answer=self.answer, answer_value="Jean"
)
partner_class = type(self.env["res.partner"])
original_write = partner_class.write
written_fields = []
def spy_write(records, vals):
written_fields.extend(vals)
return original_write(records, vals)
with patch.object(partner_class, "write", spy_write):
self.answer._mark_done()
jean = self.env["res.partner"].search([("name", "=", "Jean")])
child = self.env["res.partner"].search([("name", "=", "Child")])
self.assertEqual(child.parent_id, jean)
self.assertNotIn("parent_id", written_fields)