ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Fetch From Childtable of One Doctype to the Child Table of Another Doctype Based on Customer Name

Can a developer please guide in using a custom script to bring data from a child table of Doctype 1 into Doctype 2 based on the customer selected after pressing the button?

I’ve got a custom Doctype 1 (Measurements) which has a child table with data (Measurements of Rooms) and this doctype is linked with the Customer. Now when I create a Sales Order I want to bring this Measurement data into the child table of Sales Order that shows this customers measurement data before I create a Sales Order.

I want a button similar to Get Items From which when I press should show me a dialog similar to this:

Here the customer is entered in the form field and now it should show the measurements from the Measurements doctype. When I click on the get button, it should populate the similar child table in the Sales Order Doctype.

I was partially able to get the button and a prompt working that does fetch the Measurement list from the Measurement doctype but is not successfully populating the table. Here’s my code:

frappe.ui.form.on('Sales Order', {
    refresh: function(frm) {
        frm.add_custom_button(__("Get Measurements"), function() {
            show_m_dialog(frm);
        });
    }
});

function show_m_dialog(frm) {
   frappe.prompt([
      {'fieldname': 'measurements', 'fieldtype': 'Link', 'label': 'Customer Name', 'reqd': 1, 'options': 'Measurements'}  
   ],
   function(measurements){
      console.log(measurements.measurements);
      get_measurements(measurements.measurements);
   },
   'Get Correct Measurements',
   'Get Measurements'
  )
}

function get_measurements(frm) {
  frappe.call({
    "method": "frappe.client.get",
    "args": {
        "doctype": "measurements",
        "name": measurements
    },

    "callback": function(response) {
         // add items to your child table
         var d = response.message;
         d.items.forEach(function (item) {
            var child = cur_frm.add_child('recorded_measurements');
             frappe.model.set_value(child.doctype, child.name, 'room_name', item.room_name);
             frappe.model.set_value(child.doctype, child.name, 'area_in_sqft', item.area_in_sqft);
             frappe.model.set_value(child.doctype, child.name, 'width', item.width);
             frappe.model.set_value(child.doctype, child.name, 'height', item.height);
             frappe.model.set_value(child.doctype, child.name, 'other', item.other);
         });
         cur_frm.refresh_field('recorded_measurements');
     }
   });
}

The prompt should look like the above and it should successfully populate the child table in the Sales Order doctype. Help me figure this out, please.

This is how the child table looks like Sales Order doc and the Measurement doc:

The name of the field in Measurements Doctype is table_14 and in Sales Order as recorded_measurements

Thanks!