[Help] Fetch values from a child table to another child table

frappe.ui.form.on(“Log_Schedule”, {
“log_template”: function(frm, cdt, cdn) {
console.log(“Script triggered for ‘log_template’ field!”);
var row = locals[cdt][cdn];
if (row.log_template) {
console.log(“Selected value in ‘log_template’:”, row.log_template);
frappe.model.with_doc(“Log_Test”, row.log_template, function() {
var template_doc = frappe.model.get_doc(“Log_Test”, row.log_template);
if (template_doc) {
console.log(“Fetched Log_Test document:”, template_doc);
// Assuming ‘log_test_machines’ is the field linking to Log_Test_Machines
var machine_data = template_doc.log_test_machines || ;

      // Assuming 'log_test_readings' is another field for readings data
      var reading_data = template_doc.log_test_readings || [];

      // Update linked tables based on fetched data
      updateLinkedTables(frm, machine_data, reading_data);
    } else {
      console.error("Error: Log Template not found");
    }
  });
}

}
});

function updateLinkedTables(frm, machine_data, reading_data) {

  • console.log(“Updating machine_related_readings linked table…”);
    // Update machine_related_readings linked table
    $.each(machine_data, function(i, row) {

  • console.log(“Processing machine_related_reading data row:”, row);
    var new_machine_reading = new frappe.ui.Form({doctype: “machine_related_readings”}); // Create a new form instance
    new_machine_reading.fields.update({
    // Set relevant fields in the new form based on ‘row’ data (replace with actual field names)
    log_template: frm.doc.name,
    machine_group: row.machine_group,
    unit_measure: row.unit_measure,
    unit: row.unit,
    min_max: row.min_max,
    // Add other fields as needed
    });
    new_machine_reading.save(); // Save the new machine reading document
    });

  • console.log(“Updating other_measurable_readings linked table…”);
    // Update other_measurable_readings linked table
    $.each(reading_data, function(i, row) {

  • console.log(“Processing other_measurable_reading data row:”, row);
    var new_measurable_reading = new frappe.ui.Form({doctype: “other_measurable_readings”}); // Create a new form instance
    new_measurable_reading.fields.update({
    // Set relevant fields in the new form based on ‘row’ data (replace with actual field names)
    log_template: frm.doc.name,
    field_name: row.field_name,
    unit_of_measurement: row.unit_of_measurement,
    value: row.value,
    min_value: row.min_value,
    max_value: row.max_value,
    // Add other fields as needed
    });
    new_measurable_reading.save(); // Save the new measurable reading document
    });
    console.log(“Updated the form”);

// Refresh the form after updating linked tables

  • console.log(“Refreshing form…”);
    frm.refresh_field(“[Log_Schedule_Machines, Log_Schedule_Readings]”);
    console.log(“Refreshed form”);
    }

I have two Doctypes named Log_Test and Log_Schedule which have two separate child tables each
Log_Test_Machines, Log_Test_Readings and Log_Schedule_Machines, Log_Schedule_Readings

I have created a template in Log_Test doctype in which i have put the data of different fields in both of its child tables
machine_group, unit_measure, unit, min_max
unit_measure, unit, min_max

min_max is a data field and all other are link fields i have put the data in the Log_Test_Machines and Log_Test_Readings

when i go to log schedule doctype i have a link field in it names log_template which is linked to Log_Test
when user select a log_template the script should fetch the values of both the the child tables fields and will reflect the data in the child tables of the Log_Schedule

The script is working as it fetching the data from the child tables but its not able to reflect the data in the Log_schedule Doctype child Tables