How to fetch child table field from another doctype to Parent doctype field

Hi! I’m new to Clients Scripts and I need your help :smile:

I am creating a script for Doctype: Rental Billing. Within this “Rental Billing” Doctype, I need to select another doctype named “Rental Contract”. Rental Contract has a child table as below:

Upon selection of naming_series for “Rental Contract” in “Rental Billing” Doctype, how do you fetch the “Rate” in the child table above, and set the value of a field in “Rental Billing” as child table field “Rate” from doctype “Rental Contract”.

I wanted “Rate” field name from child table of “Rental Contract” Doctype to be fetched and set the data to “Rate per Hour” field name in “Rental Billing” Doctype.

I’m stumped! Please help.

Hi @mehmehly,

Please check the script of Fetch value in a child table field from Master

This is a perfect example for you so can check it and try it.

Thank You!

This is worked, however it doesn’t work for multi-line source child table :smiling_face_with_tear:

frappe.ui.form.on('Rental Billing', {
	refresh(frm) {
        frappe.ui.form.on('Rental Billing', {
            rental_contract: function(frm) {
                frappe.model.with_doc("Rental Contract", frm.doc.rental_contract, function() {
                    var table = frappe.model.get_doc("Rental Contract", frm.doc.rental_contract);
                    $.each(table.rental_contract_items || [], function(i, r){
                        if(r.item_code == frm.doc.rental_item_code){
                            frm.set_value("rate_per_hour", flt(r.rate));
                        }
                    });
                });  
            }
        });
	}
});