Let me add a nicely formatted example.
'Target DocType': The one you are currently working with, where the data is supposed to go.'Source DocType': The one that contains the data.link_to_source: Fieldname of the Target DocType that contains the link to'Source DocType'.target_table: Fieldname of the table in'Target DocType'.source_table: Fieldname of the table in'Source DocType'.column_name: Fieldname of the column in Child Table.
frappe.ui.form.on('Target DocType', {
link_to_source: function (frm) {
if (frm.doc.link_to_source) {
frm.clear_table('target_table');
frappe.model.with_doc('Source DocType', frm.doc.link_to_source, function () {
let source_doc = frappe.model.get_doc('Source DocType', frm.doc.link_to_source);
$.each(source_doc.source_table, function (index, source_row) {
frm.add_child('target_table').column_name = source_row.column_name; // this table has only one column. You might want to fill more columns.
frm.refresh_field('target_table');
});
});
}
},
});
What this does: Whenever the link to 'Source DocType' changes, the above script clears the target table and adds the records from the source table.
By the way, this also works for TableMultiselect fields, as the underlying data structure is the same.