Unable to set qty in stock entry from purchase receipt

Hi all,

I have a very strange behaviour when trying to set the item qty from a custom script on the stock entry.

The script should extend the “get items from” on the Stock Entry to read items from the Purchase Receipt.

This is the script:

frappe.ui.form.on("Stock Entry", {
  refresh: function(frm) {
    frm.add_custom_button( __("Purchase Receipt"),  function() {
      show_purchase_receipt_dialog(frm);
    }, __("Get items from"));
    /*frm.add_custom_button( __("Set qty"),  function() {
      set_qty(frm);
    });*/
  }
}); 

function show_purchase_receipt_dialog(frm) {
  var d = new frappe.ui.Dialog({
    'fields': [
        {'fieldname': 'ht', 'fieldtype': 'HTML'},
        {'fieldname': 'purchase_receipt', 'fieldtype': 'Link', 'options': 'Purchase Receipt'}
    ],
    primary_action: function(){
        // get values
        var data = d.get_values();
        // hide form
        d.hide();
        // load values
        load_items_from_purchase_receipt(frm, data.purchase_receipt);
    },
    primary_action_label: __('Get items')
  });
  d.fields_dict.ht.$wrapper.html( __('Select the purchase receipt that you want to get items from'));
  d.show();
}

function load_items_from_purchase_receipt(frm, purchase_receipt) {
   frappe.call({
    method:"frappe.client.get_list",
    args:{
 	doctype:"Purchase Receipt Item",
 	filters: [
 	    ["parent","=", purchase_receipt]
 	],
        fields: ["item_code", "qty", "uom"],
        parent: "Purchase Receipt",
        order_by: "idx"
    },
    callback: function(response) {
      if (response.message) {
        // remove first row
        cur_frm.get_field("items").grid.grid_rows[0].remove();
        cur_frm.refresh_field("items");
        response.message.forEach(function(item) {
          var child = cur_frm.add_child('items');
          frappe.model.set_value(child.doctype, child.name, 'item_code', item.item_code);
          frappe.model.set_value(child.doctype, child.name, 'uom', item.uom);
          frappe.model.set_value(child.doctype, child.name, 'qty', item.qty);
        });
	
        //set_qty(frm);
      }
    }
  })
}

function set_qty(frm) {
   frm.doc.items.forEach(function(item) {
          frappe.model.set_value(item.doctype, item.name, "qty", 1);
   });
}

Everything works, except that the qty will not be written along with uom and item_code. I can write it by a separate button, however, this function does not work when executed right after the automatic loading.

Any ideas qhy this fails?