"Get items from" in a new custom doctype

Hi @Anshul_Jain,

yes, sorry, two corrections: the keyword in the prompt should be “options” (smallcaps, then the dropdown/link works) and the name reference needs to be “sales_invoice.sales_invoice” (otherwise it si the object, not the name of the instance).

Try this:

frappe.ui.form.on('Test Certificate', {
    refresh: function(frm) {
        frm.add_custom_button(__("Get items from sales invoice"), function() {
            show_sinv_dialog(frm);
        });
    }
}); 

function show_sinv_dialog(frm) {
   frappe.prompt([
      {'fieldname': 'sales_invoice', 'fieldtype': 'Link', 'label': 'Sales Invoice', 'reqd': 1, 'options': 'Sales Invoice'}  
   ],
   function(sales_invoice){
      console.log(sales_invoice.sales_invoice);
      get_items_from_sinv(sales_invoice.sales_invoice);
   },
   'Get items from sales invoice',
   'Get items'
  )
}

function get_items_from_sinv(sales_invoice) {
  frappe.call({
    "method": "frappe.client.get",
    "args": {
        "doctype": "Sales Invoice",
        "name": sales_invoice
    },
    "callback": function(response) {
         // add items to your child table
         var sinv = response.message;
         sinv.items.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, 'qty', item.qty);
         });
         cur_frm.refresh_field('items');
     }
   });
}
2 Likes