Hi
I am trying to write a custom script that adds a button to Work Order, called “Stock Transfer”.
This button must create new Stock Entry and populate the items child-table in Stock Entry
( Stock Entry Detail ) , with the scrap items in the BOM related to the Work Order.
The new Stock entry is created but I have tried two approaches to populate the “items” table
but both dont work.
Here are my two attempts …
frappe.ui.form.on('Work Order', {
refresh: function(frm) {
if (!frm.doc.__islocal && frm.doc.docstatus === 1) {
// Add custom button
frm.add_custom_button('Stock Transfer', function() {
frappe.show_alert("Button", 5);
frappe.new_doc('Stock Entry', {
work_order: frm.doc.name,
stock_entry_type: 'Material Transfer',
bom_no: frm.doc.bom_no,
}, (newDoc) => {
newDoc.parenttype = 'Work Order';
newDoc.parentfield = 'required items';
newDoc.parent = frm.doc.name;
// newDoc.items[0].item_code = frm.doc.production_item;
// Get the BOM for the work order
const bom = frappe.get_doc('Bill of Materials', frm.doc.bom_no);
frappe.show_alert("Get BOM",5 );
// Iterate over the scrap items in the BOM
bom.scrap_items.forEach((scrap_item) => {
// Add a new row to the items child table in the Stock Entry
frappe.show_alert("Iterate", 5);
newDoc.items.push({
item_code: scrap_item.item_code,
item_name: scrap_item.item_name,
});
});
frappe.show_alert("Save",5 );
frappe.ui.form.make_quick_entry('Stock Entry', null, frm, newDoc);
});
});
}
}
});
Attempt 2
frappe.ui.form.on('Work Order', {
refresh: function(frm) {
if (!frm.doc.__islocal && frm.doc.docstatus === 1) {
frm.add_custom_button('Stock Transfer', function() {
frappe.show_alert("Button", 5);
frappe.new_doc('Stock Entry', {
work_order: frm.doc.name,
stock_entry_type: 'Material Transfer',
bom_no: frm.doc.bom_no,
}, (newDoc) => {
newDoc.parenttype = 'Work Order';
newDoc.parentfield = 'required items';
newDoc.parent = frm.doc.name;
newDoc.items = [];
frappe.call({
method: 'frappe.client.get_list',
args: {
parent : 'BOM',
doctype: 'BOM Scrap Item',
filters: {
parent: frm.doc.bom_no,
},
fieldname: ['item_code'],
},
callback: function(response) {
if (response.message) {
const scrapItems = response.message;
// Iterate
frappe.show_alert("Iterate",5 );
scrapItems.forEach(scrapItem => {
newDoc.items.push({
item_code: scrap_item.item_code;
item_name: scrap_item.item_name,
});
});
newDoc.refresh();
frappe.show_alert("Save", 5);
frappe.ui.form.make_quick_entry('Stock Entry', null, frm, newDoc);
}
},
});
});
});
}
}
});
Would appreciate some assistance. Thank you