I have a child table on a module A and i want to fetch the data to module B child table how can i do this ?
Hi @George_1
Kindly try this code.
Python
def copy_child_table_data(source_doc_name, target_doc_name):
# Fetch the source document (e.g., 'DocType A')
source_doc = frappe.get_doc('DocType A', source_doc_name)
# Fetch the target document (e.g., 'DocType B')
target_doc = frappe.get_doc('DocType B', target_doc_name)
# Clear existing child table rows in the target document
target_doc.child_table_b = []
# Loop through the child table in the source document and append to the target document
for row in source_doc.child_table_a:
# Create a new row in the target child table
target_doc.append('child_table_b', {
'field_name1': row.field_name1,
'field_name2': row.field_name2,
# Copy other fields as needed
})
# Save the target document
target_doc.save()
frappe.db.commit()
Java Script
frappe.ui.form.on('Target Doctype', {
refresh: function(frm) {
// Fetch source document
frappe.model.with_doc('Source Doctype', frm.doc.source_docname, function() {
let source_doc = frappe.model.get_doc('Source Doctype', frm.doc.source_docname);
// Clear the target child table
frm.clear_table('target_child_table');
// Loop through the source child table and add to the target child table
$.each(source_doc.source_child_table || [], function(index, row) {
let child_row = frm.add_child('target_child_table');
child_row.field_name1 = row.field_name1;
child_row.field_name2 = row.field_name2;
// Copy other fields as necessary
});
// Refresh the field to show updated data
frm.refresh_field('target_child_table');
});
}
});
Thank You!
thank you for this will try this out and update.