How to copy child table into new document with custom script

Hi ,

I’m trying to copy relevant fields into a new document. The parent fields are getting copied but not the child table. Any assistance will be much appreciated. Below is my code:

frappe.ui.form.on("Material Request", {
"refresh": function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frm.add_custom_button(__("Create Delivery Note"), function() {
frappe.new_doc('My Custom Doc', {
"posting_date": cur_frm.doc.posting_date,
"customer": cur_frm.doc.customer,
"items": [{"item_code": d.item_code, "item_name": d.item_name}]
});
});
}
});

Thanks

Still hoping for some suggestions here

Thanks

Dear wale,
you can find help at below link.

Hi @emre

Thanks a lot for responding but this code seems to only copy the child table into the current doc from another doctype. What I am trying to do is to copy the child table from the current doc into a new document

Any ideas ?

you can pull data to new document, you don’t need to push. if you try to create doc/docs via data from table in current doc you have to insert them to db directly.

Hi @emre

It seems strange to me that there’s no straightforward way of pushing the child table while fields on the parent doc are getting pushed without any issues? Why can’t child table fields be conveyed in the same payload? Is this a design problem?

Kind regards

In any case, thanks again for your response. Based on your comments, I solved the issue by pulling on the New Doc side (using refresh trigger). Still think it should be a more straightforward process though

Kind regards

1 Like

can you show the final code here

Sure!

Material Request Doc:

frappe.ui.form.on("Material Request", {
"refresh": function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
frm.add_custom_button(__("Create Custom Doc"), function() {
frappe.new_doc('My Custom Doc', {
"posting_date": cur_frm.doc.posting_date,
"customer": cur_frm.doc.customer,
"material_request": cur_frm.doc.name
});
});
}
});

My Custom Doc:

frappe.ui.form.on("My Custom Doc", {
"refresh": function(frm, cdt, cdn) {
if(frm.doc.__islocal && frm.doc.material_request){
frappe.model.with_doc("Material Request", frm.doc.material_request, function() {
var mcd = frappe.model.get_doc("Material Request", frm.doc.material_request);
cur_frm.clear_table("items");
$.each(mcd.items, function(i, d) {
i = frm.add_child("items");
i.item_code = d.item_code;
i.item_name = d.item_name;
i.description = d.description;
i.uom = d.uom;
i.qty = d.qty;
i.warehouse = d.warehouse;
});
cur_frm.refresh_field("items");
});
}
}
});
1 Like

Thank you for the wonderful contribution.
Through your code, I was able to do the same.
My requirement was exactly reverse of yours, but still the code helped very much.
It took some trial an error and I succeeded.