From 3562946bf3b2b4728621920a796d45a9a9f6dc9e Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Tue, 12 Jul 2022 06:18:00 +0000 Subject: [PATCH] [FIX] web: fix many2one quickcreate in legacy fields in new views Previously, legacy many2one fields and their extensions in new views were unable to quick create records, this is because the compatibility layer between the legacy fields and the relational/basic_relational model is incomplete. This commit adds code to handle this specific case Part-of: odoo/odoo#78221 --- addons/web/static/src/legacy/legacy_fields.js | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/legacy/legacy_fields.js b/addons/web/static/src/legacy/legacy_fields.js index 004cffdd07d..c0be503acef 100644 --- a/addons/web/static/src/legacy/legacy_fields.js +++ b/addons/web/static/src/legacy/legacy_fields.js @@ -7,6 +7,8 @@ import { Domain } from "web.Domain"; import legacyFieldRegistry from "web.field_registry"; import { ComponentAdapter } from "web.OwlCompatibility"; import viewUtils from "web.viewUtils"; +import { useWowlService } from "@web/legacy/utils"; +import { RPCError } from "@web/core/network/rpc_service"; const { Component, useEffect, xml } = owl; const fieldRegistry = registry.category("fields"); @@ -23,6 +25,7 @@ class FieldAdapter extends ComponentAdapter { super.setup(); this.wowlEnv = this.env; this.env = Component.env; + this.orm = useWowlService("orm"); useEffect(() => { if (!this.widgetEl || !this.widgetEl.parentElement) { return; @@ -108,8 +111,35 @@ class FieldAdapter extends ComponentAdapter { } else if (valueIds.length && "id" in valueIds[0]) { newIds = valueIds.map((r) => r.id); } - } else if ("id" in valueIds) { + } else if ("id" in valueIds && valueIds.id) { newIds = [valueIds.id]; + } else if ("id" in valueIds) { + const fieldName = this.props.fieldParams.name; + let newId; + try { + [newId] = await this.orm.call( + this.props.record.fields[fieldName].relation, + "name_create", + [valueIds.display_name], + { + context: this.props.record.getFieldContext(fieldName), + } + ); + } catch (e) { + if (!(e instanceof RPCError)) { + throw e; + } + if (payload.onFailure) { + // wrap error for guardedCatch compatibility in legacy code + payload.onFailure({ + message: e, + event: $.Event(), + legacy: true, + }); + } + return; + } + newIds = [newId]; } value = { resIds: [...record.data[name].res_ids, ...newIds],