diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js index 09a1c1041627c0cabc2bb52dc11e0e9ab59804b1..2c9b77f3da034811ee725e97009c83165e201917 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.js @@ -713,12 +713,10 @@ frappe.ui.form.on("Purchase Invoice", { }) frm.refresh_field("items"); - frappe.db.get_list("Item", {filters: {is_down_payment_item: 1}}) + frappe.db.get_list("Item", {filters: {is_down_payment_item: 1}, order_by: "down_payment_percentage DESC"}) .then(r => { - if (r.length == 1) { - frm.doc.items.forEach(line => { - frappe.model.set_value(line.doctype, line.name, "item_code", r[0].name) - }) + if (!r.exc && r.length) { + frappe.model.set_value(frm.doc.items[0].doctype, frm.doc.items[0].name, "item_code", r[0].name) } }) } diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js index ecf403a42aa432effe9912e1dc1f902b6c456bd7..19b7995523b528b18d20aa1eb186783bc388486f 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.js +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.js @@ -1035,12 +1035,10 @@ frappe.ui.form.on('Sales Invoice', { }) frm.refresh_field("items"); - frappe.db.get_list("Item", {filters: {is_down_payment_item: 1}}) + frappe.db.get_list("Item", {filters: {is_down_payment_item: 1}, order_by: "down_payment_percentage DESC"}) .then(r => { - if (r.length == 1) { - frm.doc.items.forEach(line => { - frappe.model.set_value(line.doctype, line.name, "item_code", r[0].name) - }) + if (!r.exc && r.length) { + frappe.model.set_value(frm.doc.items[0].doctype, frm.doc.items[0].name, "item_code", r[0].name) } }) }