1 of the Most FAQs: Fetching value from Source Child Table (Triggered by value in Target Child Table)

Hi community, referring to the above title, I believe ‘Custom Script for Child Table’ is one of the most asked questions in the forum as it is truly confusing and difficult to understand.

Despite my numerous attempts in creating custom scripts from this link and other posts in this forum, I still fail to create a custom script that works.

Can anyone please guide me how to fetch custom fields: ‘machine_no’ and ‘loom_no’ from ‘Stock Entry Detail’ (Stock Entry’s child table) to my custom field ‘machine_no’ and ‘loom_no’ in my custom table field ‘Batch Breakdown Table’ in Batch doctype?
.
.
.

This is how it looks in Batch form:


I want it to be triggered when Ref Doc (ref_doc) is selected
.
.
.
This is the Stock Entry Details (source child table). To fetch these 2 fields into target child table:


.
.
.
This is the target child table ‘Batch Breakdown Table’. To ‘receive’ data from source child table, Stock Entry Details when ‘ref_doc’ is triggered:

So, I really hope someone can help me in this. I need this custom script badly. Thank you in advance.

1 Like

Most easy way:

  • Add ref_doc.loom_no to the Fetch From field of your loom_no row in your child table. Do the same with machine_no.

Hard way:

  • Do something like this in your .js:
frappe.ui.form.on('Batch Breakdown Table', {
        ref_doc: function(frm, cdt, cdn){
            var row = frm.get_field("barcode_items").grid.grid_rows_by_docname[cdn];
            if(!row.doc.ref_doc || !row.doc.doc_type){
                return;
            }
            frappe.call({
                "method": "frappe.client.get",
                "args": {
                     "doctype": row.doc.doc_type,
                     "name": row.doc.ref_doc
                },
                "callback": function(r) {
                     rm = r.message;
                     if (rm) {
                    	  row.activate()
                    	  row.get_field("loom_no").set_value(rm.loom_no)
                    	  row.get_field("machine_no").set_value(rm.machine_no)
                    	  row.refresh();
                    }
                }
            });
        }
});
1 Like

Thank you for your help. Ohya, I forgot to mention why I need the custom script (the Hard Way as you referred it) because the easy way that you proposed isn’t suitable because in some cases (depending on the doctype we chose), it will not have loom_no & machine_no fields.

Then you should add some extra ifs to the above script. Tell me when you solve it.

1 Like