i have made a custom button for fetch batch no and source warehouse automatically when i made stock entry againt material transfer. so while clicking the button how to fetch the batch no automatically by using python script
Please check this Frappe Ajax Call.
And set value in js file check this Form Actions through custom Button - #2 by NCP.
yes i created button and i can able to get the batch details like batch qty but i cant able to fetch the batch id
i ahe mention the code below
import frappe
@frappe.whitelist()
def get_batch_and_warehouse(item_code, qty):
print(f"Fetching batch and warehouse details for item_code: {item_code}, qty: {qty}")
batch_details = frappe.db.sql("""
SELECT
sle.batch_no AS batch_id,
SUM(sle.actual_qty) AS batch_qty,
sle.warehouse
FROM
`tabStock Ledger Entry` sle
WHERE
sle.item_code = %s AND sle.actual_qty > 0
GROUP BY
sle.batch_no, sle.warehouse
HAVING
SUM(sle.actual_qty) > 0
ORDER BY
sle.posting_date ASC, sle.posting_time ASC
""", (item_code,), as_dict=True)
print(f"Batch details fetched: {batch_details}")
return batch_details
please share your js file code also
frappe.ui.form.on(“Stock Entry”, {
refresh: function(frm) {
if (frm.doc.stock_entry_type === “Material Transfer”) {
frm.add_custom_button(__(‘Get Batch and Warehouse’), function() {
fetch_item_code_list(frm);
}, __(“Fetch Item Details”));
}
}
});
function fetch_item_code_list(frm) {
frm.doc.items.forEach(function(row) {
console.log(“Fetching batch and warehouse details for item:”, row.item_code , row.qty);
frappe.call({
method: "training.custom.material_transfer.get_batch_and_warehouse",
args: {
item_code: row.item_code,
qty: row.qty
},
callback: function(r) {
console.log("Server response:", r);
if (!r.exc && r.message) {
if (r.message.length > 0) {
let batch_detail = r.message[0];
console.log("Batch detail fetched:", batch_detail);
frappe.model.set_value(row.doctype, row.name, "batch_no", batch_detail.batch_id);
frappe.model.set_value(row.doctype, row.name, "s_warehouse", batch_detail.warehouse);
frappe.model.set_value(row.doctype, row.name, "actual_qty", batch_detail.batch_qty);
} else {
frappe.msgprint(__('No batch available for Item {0}', [row.item_code]));
}
frm.refresh_field("items");
} else {
frappe.msgprint(__('Failed to fetch batch and warehouse details for Item {0}', [row.item_code]));
}
}
});
});
}
Hi, But you can easily get details from batch doctype, why create complexity