I’ve been trying to fetch the “Supplier Invoice Number” in the child table “Payment Entry Reference” of “Payment Entry” DOCTYPE. I have tried several options like Fetch From (it does not have Purchase Invoice DocType under Select DocType)
But I figured it out by myself already. Here’s an explanation for what I did in-case someone else needs to do the same.
This is a custom client script, which fetches Supplier’s Invoice Date in the child table called Payment Entry Reference in the Payment Entry Form, by referencing it to reference_name
Note: The field gets updated on saving the payment entry form or refreshing the form, (I had to do this because my system is hosted on Frappe Cloud and I cannot modify it to trigger the “fetch_bill_dates” function on pressing “Get Outstanding Invoices / Get Outstanding Orders”)
frappe.ui.form.on("Payment Entry", {
refresh: function(frm) {
// Refresh or load the form to ensure proper setup
frm.trigger('fetch_bill_dates');
},
references_add: function(frm, cdt, cdn) {
// This is triggered when a new row is added to the references table
frm.trigger('fetch_bill_dates');
},
after_save: function(frm) {
// Ensure bill dates are fetched after saving
frm.trigger('fetch_bill_dates');
},
fetch_bill_dates: function(frm) {
// Loop through each row in the references table
$.each(frm.doc.references || [], function(i, d) {
// Check if the reference_doctype is "Purchase Invoice"
if (d.reference_doctype === "Purchase Invoice" && d.reference_name) {
// Fetch the bill_date from the Purchase Invoice
frappe.db.get_value("Purchase Invoice", {"name": d.reference_name}, "bill_date", function(value) {
if (value && value.bill_date) {
frappe.model.set_value(d.doctype, d.name, "custom_bill_date", value.bill_date);
frm.refresh_field("references");
}
});
}
});
}
});