Fetch Child table values to another child table

Good morning everyone,

Firstly i want to say i just read the following topic HOW TO: Fetch Child Tables, but since i’m new to this practice i don’t know how to set the fields correctly.

Now i’ll explain my use case: Simply i’d like to add in the Production Order, once i select the Item to Manufacture, the correspondent BOM Item (first level only, not exploded). So i created a custom field table type (“bom_item”) which is linked to child doctype “BOM Item 2” (i read somewhere that the child table i want to fetch the values to must be different from the child table from where i get them).

I used the following code, where:
“production_item” is the field used as trigger,
“bom_item” is the custom field used as table (where i want to fetch the BOM item of the item i select in production_item),
“bom_no” is the default field (in Production Order DocType) where appear the BOM related to the item i choose to manufacture,
“items” is the default field (in BOM DocType) which contains the BOM Item child table,
“code”, “name1”, “uom” and “qty” are the fields inside the BOM Item 2 child table.

frappe.ui.form.on("Production Order", "production_item", function(frm) {
frappe.model.with_doc("bom_no", frm.doc.production_item, function() {
var tabletransfer= frappe.model.get_doc("bom_no", frm.doc.production_item)
$.each(qmtable.items, function(index, row){
d = frm.add_child("BOM Item 2");
d.item_code = row.code;
d.item_name = row.name1;
d.stock_uom = row.uom;
d.qty = row.qty;
cur_frm.refresh_field("BOM Item 2");
})
});
});

Hope i explained the case clearly and that someone can help me.

1 Like

try:
var tabletransfer= frappe.model.get_doc("BOM", frm.doc.production_item)

Where are u using tabletransfer?

If i change to BOM as you said, the following error shows (i changed bom_no to BOM both in 2nd and 3rd row):

bom error

PS i even realize that always in 2nd and 3rd rows, i had to reset to “trigger” instead of “production_item” (i had to change it only in the 1st row).

Not sure what are you asking me here. I get the code as it was on the topic i linked in the first post and tried to adapt it to my case.

But i want to trasfer the BOM Item’s table relative to the BOM of the selected item to the purchase order.

above should be:
$.each(tabletransfer.child_table_name, function(index, row){

Actually the code is this

frappe.ui.form.on("Production Order", "production_item", function(frm) {
frappe.model.with_doc("BOM", frm.doc.trigger, function() {
var tabletransfer= frappe.model.get_doc("BOM", frm.doc.production_item)
$.each(tabletransfer.items, function(index, row){
d = frm.add_child("BOM Item 2");
d.item_code = row.code;
d.item_name = row.name1;
d.stock_uom = row.uom;
d.qty = row.qty;
cur_frm.refresh_field("BOM Item 2");
})
});
});

And with

frappe.model.with_doc("BOM", frm.doc.trigger, function() {
var tabletransfer= frappe.model.get_doc("BOM", frm.doc.production_item)

i got thie error showed above (BOM BOM not found)

while if i use

frappe.model.with_doc("bom_no", frm.doc.trigger, function() {
    var tabletransfer= frappe.model.get_doc("bom_no", frm.doc.production_item)

i got the following error:

Traceback (most recent call last):
  File "/home/erp/frappe-bench/apps/frappe/frappe/desk/form/load.py", line 32, in getdoc
    doc = frappe.get_doc(doctype, name)
  File "/home/erp/frappe-bench/apps/frappe/frappe/__init__.py", line 596, in get_doc
    return frappe.model.document.get_doc(arg1, arg2)
  File "/home/erp/frappe-bench/apps/frappe/frappe/model/document.py", line 48, in get_doc
    controller = get_controller(doctype)
  File "/home/erp/frappe-bench/apps/frappe/frappe/model/base_document.py", line 34, in get_controller
    module = load_doctype_module(doctype, module_name)
  File "/home/erp/frappe-bench/apps/frappe/frappe/modules/utils.py", line 173, in load_doctype_module
    raise ImportError, 'Module import failed for {0} ({1})'.format(doctype, module_name)
ImportError: Module import failed for bom_no (frappe.core.doctype.bom_no.bom_no)

Traceback (most recent call last):
  File "/home/erp/frappe-bench/apps/frappe/frappe/app.py", line 55, in application
    response = frappe.handler.handle()
  File "/home/erp/frappe-bench/apps/frappe/frappe/handler.py", line 19, in handle
    execute_cmd(cmd)
  File "/home/erp/frappe-bench/apps/frappe/frappe/handler.py", line 40, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/home/erp/frappe-bench/apps/frappe/frappe/__init__.py", line 896, in call
    return fn(*args, **newargs)
  File "/home/erp/frappe-bench/apps/frappe/frappe/desk/form/load.py", line 32, in getdoc
    doc = frappe.get_doc(doctype, name)
  File "/home/erp/frappe-bench/apps/frappe/frappe/__init__.py", line 596, in get_doc
    return frappe.model.document.get_doc(arg1, arg2)
  File "/home/erp/frappe-bench/apps/frappe/frappe/model/document.py", line 48, in get_doc
    controller = get_controller(doctype)
  File "/home/erp/frappe-bench/apps/frappe/frappe/model/base_document.py", line 34, in get_controller
    module = load_doctype_module(doctype, module_name)
  File "/home/erp/frappe-bench/apps/frappe/frappe/modules/utils.py", line 173, in load_doctype_module
    raise ImportError, 'Module import failed for {0} ({1})'.format(doctype, module_name)
ImportError: Module import failed for bom_no (frappe.core.doctype.bom_no.bom_no)

Here is my Purchase Order customize window

So the problem actually is that i miss to recognize the DocTypeA (Source DocType The document that holds the child table i am getting values from)

I solved with the following code, in Production Order-Client, where ausiliare is a custom field that get automatically filled once selected the item to manufacture.
Can’t understand why, if i put as trigger production_item field, it doesn’t work.

frappe.ui.form.on("Production Order", {
    "ausiliare": function(frm) {
        frappe.model.with_doc("BOM", frm.doc.ausiliare, function() {
            var tabletransfer= frappe.model.get_doc("BOM", frm.doc.ausiliare)
            $.each(tabletransfer.items, function(index, row){
                d = frm.add_child("items");
                d.code = row.item_code;
                d.name1 = row.item_name;
		    d.uom = row.stock_uom;
                d.qty = row.qty;
                frm.refresh_field("items");
            });
        });
    }
});

It work almost perfectly. The only defect is that once i select one item it get the BOM Item as it should, but if i change item without refresh the page, it doesn’t update the BOM Item as it should but it adds the new rows (of the new item i changed) above the previous ones.

Thanks to everyone for support.

1 Like