Pulling Child Table

This topic has been brought up several times. The question of how to pull a child table from one form to another using a custom script. This is not working for me so far:

Current Document Details

Source DocType: QuoteMaster
Source Child Table Field Name: items
Source Child Table DocType: BOM Item

Target DocType: Sales Order
Target Link Field: quotemaster
Target Child Table Field Name: qm_items
Target Child Table DocType: BOM Item

Current Custom Script:
frappe.ui.form.on("Sales Order", "quotemaster", function(frm) {
    frappe.model.with_doc("QuoteMaster", frm.doc.quotemaster, function() { 
        d = frm.add_child("qm_items");
        quotemaster.items.forEach(function(row){
        d.item_code = row.item_code;
        d.qty = row.qty;
        });
    });
    cur_frm.refresh_field("qm_items");
});

Currently, this does not pull any items from the QuoteMaster table. However, on save a blank line is added to the Sales Order Table.
@max_morais_dmm

Hi @cpurbaugh,

Try below code

frappe.ui.form.on(“Sales Order”, “quotemaster”, function(frm) {
frappe.model.with_doc(“QuoteMaster”, frm.doc.quotemaster, function() {
$.each(frm.doc.items, function(index, row){
d = frm.add_child(“qm_items”);
d.item_code = row.item_code;
d.qty = row.qty;
})
});
cur_frm.refresh_field(“qm_items”);
});

Thanks, Rohit

1 Like

@rohit_w this almost works! except that it’s pulling from the sales order item table instead of the quotemaster item table.

got it!

frappe.ui.form.on("Sales Order", "quotemaster", function(frm) {
frappe.model.with_doc("QuoteMaster", frm.doc.quotemaster, function() {
var qmtable = frappe.model.get_doc("QuoteMaster", frm.doc.quotemaster)
$.each(qmtable.items, function(index, row){
d = frm.add_child("qm_items");
d.item_code = row.item_code;
d.qty = row.qty;
cur_frm.refresh_field("qm_items");
})
});
});

Thank you @rohit_w & @max_morais_dmm for your help on this! I’ll be doing a writeup on how this works so hopefully other people can figure this out!

5 Likes