fix(linked documents): pass link specific fieldname to button so that only that field gets populated while creating a new doc

This commit is contained in:
Akash Tom 2026-02-23 15:21:48 +05:30
parent b7214c6957
commit 20a946d7f5
4 changed files with 40 additions and 11 deletions

View file

@ -793,11 +793,21 @@ class Meta(Document):
group.get("items").append(doctype)
link.added = True
# Add fieldname to transaction group for external links
if not link.is_child_table:
if "fieldnames" not in group:
group["fieldnames"] = {}
group["fieldnames"][link.link_doctype] = link.link_fieldname
if not link.added:
# group not found, make a new group
data.transactions.append(
dict(label=link.group, items=[link.parent_doctype or link.link_doctype])
)
new_group = dict(label=link.group, items=[link.parent_doctype or link.link_doctype])
# Add fieldname to new transaction group for external links
if not link.is_child_table:
new_group["fieldnames"] = {link.link_doctype: link.link_fieldname}
data.transactions.append(new_group)
if not data.fieldname and link.link_fieldname:
data.fieldname = link.link_fieldname

View file

@ -270,6 +270,13 @@ frappe.ui.form.Dashboard = class FormDashboard {
if (d.label == group.label) {
group_added.push(d.label);
group.items.push(...d.items);
if (d.fieldnames) {
if (!group.fieldnames) {
group.fieldnames = {};
}
Object.assign(group.fieldnames, d.fieldnames);
}
}
});
});
@ -335,7 +342,9 @@ frappe.ui.form.Dashboard = class FormDashboard {
// bind new
transactions_area_body.find(".btn-new").on("click", function () {
me.frm.make_new($(this).attr("data-doctype"));
const doctype = $(this).attr("data-doctype");
const fieldname = $(this).attr("data-fieldname");
me.frm.make_new(doctype, fieldname);
});
this.data_rendered = true;

View file

@ -2007,7 +2007,7 @@ frappe.ui.form.Form = class FrappeForm {
}
}
make_new(doctype) {
make_new(doctype, fieldname) {
// make new doctype from the current form
// will handover to `make_methods` if defined
// or will create and match link fields
@ -2021,7 +2021,7 @@ frappe.ui.form.Form = class FrappeForm {
let new_doc = frappe.model.get_new_doc(doctype, null, null, true);
// set link fields (if found)
me.set_link_field(doctype, new_doc);
me.set_link_field(doctype, new_doc, fieldname);
frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
// frappe.set_route('Form', doctype, new_doc.name);
@ -2029,16 +2029,24 @@ frappe.ui.form.Form = class FrappeForm {
}
}
set_link_field(doctype, new_doc) {
set_link_field(doctype, new_doc, fieldname) {
let me = this;
frappe.get_meta(doctype).fields.forEach(function (df) {
if (df.fieldtype === "Link" && df.options === me.doctype) {
const isLinkToParent = df.fieldtype === "Link" && df.options === me.doctype;
if (fieldname) {
if (df.fieldname === fieldname && isLinkToParent) {
new_doc[df.fieldname] = me.doc.name;
}
return;
}
if (isLinkToParent) {
new_doc[df.fieldname] = me.doc.name;
} else if (["Link", "Dynamic Link"].includes(df.fieldtype) && me.doc[df.fieldname]) {
new_doc[df.fieldname] = me.doc[df.fieldname];
} else if (df.fieldtype === "Table" && df.options && df.reqd) {
let row = new_doc[df.fieldname][0];
me.set_link_field(df.options, row);
me.set_link_field(df.options, new_doc[df.fieldname][0]);
}
});
}

View file

@ -9,6 +9,7 @@
</div>
{% for (let j=0; j < transactions[i].items.length; j++) { %}
{% let doctype = transactions[i].items[j]; %}
{% let fieldname = (transactions[i].fieldnames && transactions[i].fieldnames[doctype]) || transactions[i].fieldname; %}
<div class="document-link" data-doctype="{{ doctype }}">
<div class="document-link-badge" data-doctype="{{ doctype }}">
<a class="badge-link">{{ __(doctype) }}</a>
@ -19,7 +20,8 @@
</span>
{% if !internal_links[doctype] %}
<button class="btn btn-new btn-secondary btn-xs icon-btn hidden"
data-doctype="{{ doctype }}">
data-doctype="{{ doctype }}"
data-fieldname="{{ fieldname }}">
<svg class="icon icon-sm"><use href="#icon-add"></use></svg>
</button>
{% endif %}