Please Debug/Review My Custom Script Code

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

//creates a modal dialog with a link field fetching from Measurement doctype
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'
    );
}

//getting measurements from the child table of the Measurement doctype (parent)
function get_measurements(measurements) {
 frappe.call({
    method: "frappe.client.get_list",
    args:{
        doctype:"Measurement Table",
        parent: "Measurements",
        fields: ["room_name", "width", "height", "area_in_sqft"],
        limit: 99
    },
    callback: function(response) {
      if (response.message) {
        response.message.forEach(function(entry) {
          //frappe.msgpirnt(entry.name);
          var child = cur_frm.add_child('recorded_measurements');
          frappe.model.set_value(child.doctype, child.name, 'room_name', entry.room_name);
          frappe.model.set_value(child.doctype, child.name, 'width', entry.width);
          frappe.model.set_value(child.doctype, child.name, 'height', entry.height);
          frappe.model.set_value(child.doctype, child.name, 'area_in_sqft', entry.area_in_sqft);
          console.log(response.message);
        });
        cur_frm.refresh_field('recorded_measurements');
      }
    }
  });
}

Although the script is successfully fetching data from a child table (namely Measurement Table) which is in a parent doctype (namely Measurements), but there are some flaws I am unable to rectify:

  1. After I press the button and the modal dialog window pops up, the link quickly field fetches all saved Measurements in the Measurements Doctype, but once I select one among many different items in the link field, it still fetches data from all other saved Measurements and brings them into the table. (Not only the one I selected but all that are saved in the doctype).
  2. This data from everything saved in the Measurements doctype is inserted in the table multiple times on top of one another (although I selected only one item in the link field).
  3. Surprisingly it also fetches from seemingly deleted (which are not there in the doctype) Measurements too (which is quite weird).

These are the 3 main problems I am getting with this script. I would be thankful for help from the developers here.

Moreover, I am also unable to filter the link field to match with the customer selected in the Sales Order at that time, tried to use the filter in the frappe.client.get_list but that gives an error.

not a developer so won’t be able to help you out on this. But curious as i went through the code, is this for real estate or interior custom quotation to sales order solution?

I was able to find a solution, will post it here for the community soon :slight_smile:

And yes this is for an interior design/home & living company, they capture customer-specific measurements and other details which have to be inserted into the sales order, hence the child table query.