HOW TO: Fetch Child Tables

Thanks, it worked for me.

Hi @Juri How can I integrate these codes if I need to fetch data on a parent doctype and encode it on the child table using a dynamic link?

I was trying to fetch the data on a custom field on the parent doctype of the Sales Invoice then encode it on the child table payment entry references.

This is same on how the Supplier Invoice No. works. When the dynamic link is a Purchase Invoice, a supplier invoice no. field will be visible.

This guide might help

@Vesper_Solutions thank you for sharing the link. However, I had a hard time making the codes work with a dynamic link as a trigger or to fetch the source doctype of it :frowning:

hi @ponyooooo,

Excuse me, I’ve been too busy with other construction sites. Did the code from Vesper_Solutions help?

Hello @rmeyer

I tried your script and it worked perfect but i was wondering how best i could modify the scripts to add more columns and the respective data will be in the same row. Below is my script

on the custom scripts, i added the following column from the source table called “concreting” which are “job”, “description”, “unit”. it actually imported them but they are not on the same row as shown below

1 Like

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.