HOW TO: Fetch Child Tables

Hi Juri,

No. I am not sure if the cause of the version of my ERPNext. My current version is

ERPNext: v12.4.2 (version-12)
Frappe Framework: v12.2.1 (version-12)

:frowning:

Hi @Chibuzor_Derrick may I request if you could paste the code you have used please? So I can try the codes also? Thank you. :laughing:

@ponyooooo

frappe.ui.form.on('Memo', {
    project: function (frm) {
        if (frm.doc.project) {
            frm.clear_table('table');
            frappe.model.with_doc('Task', frm.doc.project, function () {
                let source_doc = frappe.model.get_doc('Task', frm.doc.project);
                $.each(source_doc.concreting, function (index, source_row) {
                    frm.add_child('table').job = source_row.job;// this table has only one column. You might want to fill more columns.
                    frm.refresh_field('table');
                });
            });
        }
    },
});

@rmeyer this is the source doctype showing the source child table in a section called labor

@Chibuzor_Derrick try something like this:

frappe.ui.form.on("Memo", {
	project: function (frm) {
		if (frm.doc.project) {
			frm.clear_table("table");
			frappe.model.with_doc("Task", frm.doc.project, function () {
				const source_doc = frappe.model.get_doc("Task", frm.doc.project);
				for (const source_row of source_doc.concreting) {
					const target_row = frm.add_child("table");
					target_row.job = source_row.job;
					target_row.description = source_row.description;
					target_row.unit = source_row.unit;
					// ...
					frm.refresh_field("table");
				}
			});
		}
	},
});
10 Likes

@rmeyer. it worked perfectly well. many thanks

2 Likes

Hi,
If could I have some help please,
In Doctype “Project”, I have created a custom field ‘total_committed_amount’, I need to fetch the total value from field “amount” in Doctype “Purchase Order Item”,
I have seen this example and I have tried to apply it,
Any help would be very welcome,

frappe.ui.form.on('Project', {
    Purchase_Order: function (frm) {
        if (frm.doc.Purchase_Order) {
            frm.clear_table('total_committed_amount');
            frappe.model.with_doc('Purchase_Order_Item', frm.doc.Purchase_Order, function () {
                let source_doc = frappe.model.get_doc('Purchase_Order_Item', frm.doc.Purchase_Order);
                $.each(source_doc.Project, function (index, source_row) {
                    frm.add_child('total_committed_amount').Amount = source_row.Amount  ; // this table has only one column. You might want to fill more columns.
                    frm.refresh_field('total_committed_amount');
                });
            });
        }
    },
});

Hey @rmeyer your help is highly appreciated. This works perfectly fine. :pray:

1 Like

Try the code given by @rmeyer. It works fine.

i want fetch filed from child table to filed out side like this…

Please tell your complete use case.

Hi Ponyooooo
Please I’m trying the sum total of the same field have in your table
I mean how did you generate the total amount = Outstanding
Any codes pls

Hi,

I used the following code. However, I stopped using it since this code stops or gives me an error each time the frappe cloud tries to upgrade our erpnext version. It was related on the total_payment field which you will store the total amount.

frappe.ui.form.on(‘Payment Schedule’, {
payment_amount: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var total_payment = 0;
frm.doc.customized_payment_schedule.forEach(function(d) {
total_payment += d.payment_amount;
}
);
frm.set_value(“total_payment”, total_payment);
frm.refresh_field(“total_payment”);
},
customized_payment_schedule_remove: function(frm, cdt, cdn) {
var d = locals[cdt][cdn];
var total_payment = 0;
frm.doc.customized_payment_schedule.forEach(function(d) {
total_payment += d.payment_amount;
}
);
frm.set_value(“payment_amount”, payment_amount);
frm.refresh_field(“payment_amount”);
}
});

If you found the error on the code, let me know so I can use it again. :slight_smile:

Thanks.

Sure thing,
Thanks


still not working.

awesome

My child table has a field column called “amount”. I want the amount column to Add up and display the total amount on the parent target DocType.
below is my script but not working;
frappe.ui.form.on(“Subcontractor Payment Item”,{
amount: function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frm.doc.subcontractor_payment_item.forEach(function (d) { total += d.amount; });
cur_frm.set_value(“total_amount”, total);
refresh_field(“total_amount”);
},

subcontractor_payment_item_remove: function(frm, cdt, cdn){
    var d = locals[cdt][cdn];
    var total = 0;
    frm.doc.subcontractor_payment_item.forEach(function (d) { total += d.amount; });
    cur_frm.set_value("total_amount", total);
    refresh_field("total_amount");
}

});

pls i need your help

I have created a custom Doctype in quality management named “NEW TC”. Settings as under:

In the row #5 , I need item table from sales order to be fetched when i select the sales order above.

Please advise your valuable inputs as i had been trying for 48 hrs + but all in vain. Since i am not a computer language expert or programmer, it is very difficult for me.

I use this , this gives me whole data what if I want to get only one field’s data. then what should I use to get the specific field from a doctype list.

You’ll have to do basic custom scripting.
Refer to below link where we have setup similar coding to get data from child table of another doctype.