I have a usecase where i need to populate data to a childtable that’s shown in parent doctype on click of a button which calls server script and gets the data
frappe.ui.form.on("Physical Audit", {
refresh: function (frm) {
// Check if docstatus is "Inventory Pending"
if (frm.doc.docstatus === 0 && frm.doc.workflow_state === "Inventory Check Pending") {
frm.add_custom_button("Fetch Inventory", () => {
frappe.confirm(
"This will fetch data from Redshift and update the Inventory Details table. Are you sure?",
() => {
frappe.call({
method: "audit.audit.doctype.physical_audit.physical_audit.fetch_and_populate_inventory",
args: { doc: frm.doc },
callback: function (response) {
console.log("Response from server:", response);
if (response.message && response.message.inventory_details) {
console.log("Setting child table data:", response.message.inventory_details);
// Clear existing child table data
frm.clear_table("inventory_details");
// Add rows dynamically with proper flags
response.message.inventory_details.forEach((row) => {
let child = frm.add_child("inventory_details");
frappe.model.set_value(child.doctype, child.name, "material_id", row.material_id);
frappe.model.set_value(child.doctype, child.name, "material_name", row.material_name);
frappe.model.set_value(child.doctype, child.name, "system_qty", row.system_qty);
frappe.model.set_value(child.doctype, child.name, "physical_qty", row.physical_qty);
});
// Refresh the field to reflect the changes in the UI
frm.refresh_field("inventory_details");
frappe.show_alert({
message: `${response.message.inventory_details.length} rows added to Inventory Details.`,
indicator: "green"
});
} else {
frappe.msgprint("No inventory data returned.");
}
},
error: function (error) {
console.error("Error occurred:", error);
frappe.msgprint({
title: "Error",
message: error.message || "An error occurred while fetching data.",
indicator: "red"
});
}
});
}
);
});
}
}
});
above client script is working fine and injecting data to childtable and shown to the user but when the parent doctype is saved the injected data is not getting saved.
Not sure what i am missing