Try ERPNext Buy Support Partners Foundation Foundation Members

"Get items from" in a new custom doctype

Hi! I have added a new doctype in Selling module by the name of “Test Certificate”. I need to pull items from existing sales invoice, such as customer name, item name, date etc.
Is there a way to do that? Tried searching in the forum, couldnt find exactly what I was looking for.
This is what I have tried so far:

Is there any way to add “get items from” button in my custom doctype?

Hi @Anshul_Jain,

you can add a custom script also for your custom doctype. With this you can add a button, have the button display a dialog to select a Sales Invoice from which it will then pull the items. Make sure your doctype has a child table that allows to store items.

Something like

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){
      get_items_from_sinv(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');
     }
   });
}

Hope this helps.

2 Likes

Hi @lasalesi

I am unable to get the list of sales invoice from this script. Even when I enter the desired sales invoice number, the page throws an error which reads:
Sales Invoice {“sales_invoice”:“INVS-00203”} not found
The resource you are looking for is not available

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');
     }
   });
}
1 Like

Thank you so so much for this help even im not the one asking you have solved many of my problem. Thank you!

Hi , I’ve similar issue and Not solved, may you please help

I’ve a Customs doctype “Project Track Record” which includes child table under name SCOPE with the same fields names of sales order items, I need to use “Get Items from Sales Order” and used below custom script but get nothing:

frappe.ui.form.on(‘Project Track Record’, {
refresh: function(frm) {
frm.add_custom_button(__(“Get items from sales order”), function() {
show_so_dialog(frm);
});
}
});

function show_so_dialog(frm) {
frappe.prompt([
{‘fieldname’: ‘sales_order’, ‘fieldtype’: ‘Link’, ‘label’: ‘Sales Order’, ‘reqd’: 1, ‘options’: ‘Sales Order’}
],
function(sales_order){
console.log(sales_order.sales_order);
get_items_from_so(sales_order.sales_order);
},
‘Get items from sales order’,
‘Get items’
)
}

function get_items_from_so(sales_order) {
frappe.call({
“method”: “frappe.client.get”,
“args”: {
“doctype”: “Sales Order”,
“name”: sales_Order
},
“callback”: function(response) {
// add items to your child table
var so = response.message;
so.items.forEach(function (item) {
var child = cur_frm.add_child(‘items’);
frappe.model.set_value(child.doctype, child.scope, ‘item_code’, item.item_code);
frappe.model.set_value(child.doctype, child.scope, ‘item_name’, item.item_name);
frappe.model.set_value(child.doctype, child.scope, ‘description’, item.description);
frappe.model.set_value(child.doctype, child.scope, ‘stock_uom’, item.stock_uom);
frappe.model.set_value(child.doctype, child.scope, ‘price_list_rate’, item.price_list_rate);
});
cur_frm.refresh_field(‘items’);
}
});
}