Fetch Child table form one doctype to other doctype

Dear Community,
I had two doctype with same child table but need to fetch based on doctype 1.
Doctype A is Work Handover
Child table A is handover_details
Doctype B is Leave Application
Child table B is work_handover

both child table are same but in different field name if i create work handover and in leave application i select the work handover detail it should automatically fetch the work handover table.

Doctype A

Doctype B


while selecting Select work handover child table should need to fetch from Doctype A.

in Doctype A (Work handover)

in Doctype B (Leave Application)

Doctype A,B common child table (Work handover)

1 Like

Hi @Mohamed2335,

Please check the syntax:

frappe.ui.form.on('DocType B', {
	doctype_a_link_fieldname_in_doctype_b: function(frm) {
        if (frm.doc.doctype_a_link_fieldname_in_doctype_b) {
            frappe.call({
                method: 'frappe.client.get',
                args: {
                    doctype: "DocType A",
                    filters: {
                        name: frm.doc.doctype_a_link_fieldname_in_doctype_b
                    }
                },
                callback: function(r) {
                    if (r.message) {
                        frm.set_value('parent_field_of_doctype_b', r.message.field_name);
                        // other parent fieldname
                
                        frm.clear_table('doctype_b_child_table_name');
                        
                        r.message.doctype_a_child_table_fieldname.forEach(function(item) {
                            var child = frm.add_child('doctype_b_child_table_name');
                            child.doctype_b_child_table_fieldname = item.doctype_a_child_table_fieldname;
                            // other child table fieldname
                        });
                        frm.refresh_field('doctype_b_child_table_name');
                    }
                }
            });
        }
	}
});

Pls help to correct my code.

frappe.ui.form.on(‘Leave Application’, {
leave_application_link_fieldname_in_work_handover: function(frm) {
if (frm.doc.leave_application_link_fieldname_in_work_handover) {
frappe.call({
method: ‘frappe.client.get’,
args: {
doctype: “Work Handover”,
filters: {
name: frm.doc.doc.leave_application_link_fieldname_in_work_handover
}
},
callback: function(r) {
if (r.message) {
frm.set_value(‘work_details’, r.message.work_details);
frm.set_value(‘handed_over’, r.message.handed_over);
frm.set_value(‘status’, r.message.status);
// other parent fieldname
frm.clear_table(‘work_handover’);
r.message.leave_application_work_handover.forEach(function(item)
{
var child = frm.add_child(‘work_handover’);
child.work_handover= item.handover_details;
// other child table fieldname
});
frm.refresh_field(‘work_handover’);
}
}
});
}
}
});

Ensure that you check the field name, document type name, and child table field name, then adjust them accordingly in the script.

frappe.ui.form.on('Leave Application', {
	select_work_handover: function(frm) {
        if (frm.doc.select_work_handover) {
            frappe.call({
                method: 'frappe.client.get',
                args: {
                    doctype: "Work handover",
                    filters: {
                        name: frm.doc.select_work_handover
                    }
                },
                callback: function(r) {
                    if (r.message) {
                        frm.set_value('employee', r.message.employee);
                        // other parent fieldname
                
                        frm.clear_table('work_handover');
                        
                        r.message.work_handover.forEach(function(item) {
                            var child = frm.add_child('work_handover');
                            child.work_details = item.work_details;
                            // other child table fieldname
                        });
                        frm.refresh_field('work_handover');
                    }
                }
            });
        }
	}
});

Thanks for your response with small adjustments it works

frappe.ui.form.on('Leave Application', {
    select_work_handover: function(frm) {
        if (frm.doc.select_work_handover) 
            frappe.call({ 
                method: 'frappe.client.get', 
                args: {
                    doctype: "Work Handover", 
                    filters: {
                        name: frm.doc.select_work_handover
                    }
                },
                callback: function(r) { 
                    if (r.message) { 
                        // Clearing existing table rows
                        frm.clear_table('work_handover');
                        // Iterating through the fetched data from handover_details and populating the work_handover child table
                        r.message.handover_details.forEach(function(item) {
                            var child = frm.add_child('work_handover'); 
                            child.work_details = item.work_details; 
                            child.handed_over = item.handed_over;
                            child.status = item.status;
                            // other child table fieldname
                        });
                        frm.refresh_field('work_handover'); 
                    }
                }
            });
        }
    }
});

I have given the reference of the video here, maybe it will help someone.

3 Likes
How to fetch fields from one doctype to another
Fetch data from child table in doctype to another child table in another doctype
`Set Value` does not triggering `Fetch From` Functionality in Child Doc
Fetching the Document ID from one DocType to Another
How to set select data source
Fatch Data from custom doctype to table
Leave History Fetch from child table in Leave application
When creating a Custom Field, In the "Fetch From" drop down, it doesn't display the DocType List
Want to add Link field in Non Conformance form showing customer full name from customer DocType
Fetching data from child table to another child table
How to fetch Job offer yearly field in to employee cost to company field
Fetching Employee Data
How to populate a Field based on selected Link's Child Table
How to automatically fetch data in quotation item child table from item custom feilds?
Custom field in Customer to get balance data account and loyalty point
How can I create another doctype from and pull the data from the lead?
How fetch one field data from one doctype to another doctype
Pre-populating a field from another DocType
Responsive Reload Button
Can we show table through link if we select an bundle and table is shown
Is it possible to fetch table as we can fetch other fields (see comments)
I need autoupdate when event is saved it should autofetch in Activity log(Issue) childtable
Fetch data from linked table linked to a child table
Grab a value from another doctype, which was in another doctype defined in a field
Data transfer from one doctype to another
Fetch From Is not working in Custom Doctype
Create doctype time fetch from in not visible filed
How to create a template for a doctype and append those data in another doctype
Total not calculating
How to display details of a child doctype of one doctype to anothe submittable doctype
Link child table to another form
Fetch from doctype not show