Custom button and add_fetch

Hello,

I am trying to add a custom button on a doctype that will trigger the add_fetch function.

I am able to use the add_fetch alone, use the button to display an alert or to fill a field with text but not to fetch data from another doctype.

Here is my script today:

My button is called “get_po_details” and I chose the related PO in the “related_po” field (link type with POs) I created.

cur_frm.cscript.get_po_details = function(doc) {
cur_frm.add_fetch(‘related_po’,‘country_of_origin’,‘country_of_origin’);
cur_frm.add_fetch(‘related_po’,‘destination’,‘destination’);
cur_frm.add_fetch(‘related_po’,‘destination_2’,‘destination_2’);
}

Am I missing something?

Add fetch is not designed to work that way:

try this:

frappe.ui.form.on("[doctype]", "get_po_details", function(frm) {
  frappe.model.with_doc("Purchase Order", frm.doc.related_po, function() { 
     var po = frappe.model.get_doc("Purchase Order", frm.doc.related_po);
     frm.set_value("country_of_origin", po.country_of_origin);
     ...
  });
});
1 Like

Works perfectly thanks !

I am trying to fetch item_code as well but it is not in the Purchase Order, what is the correct way to fetch data from child doctype?

Thanks

loop over po.items in the earlier script.

with_doc loads the entire document.